l = ['sone.com']
for a in l:
if '#' and '.' in a:
print('ok')
Result is coming to be ok. Why?
if '#' and '.' in a:
print('ok')
is causing the error, change it to
if '#' in a and '.' in a:
print('ok')
when you do if '#' on its own, it returns true.
Think of if '#' is the same as saying if '#' = '#'.
and when you use and the two variables need to be set individual condtions so ...
if '#' in a and '.' in a
notice the if '#' in a & '.' in a
Related
I have a string:
s='articles[zone.id=1].comments[user.status=active].user'
Looking to split (via split(some_regex_here)). The split needs to occur on every period other than those inside the bracketed substring.
Expected output:
["articles[zone.id=1]", "comments[user.status=active]", "user"]
How would I go about this? Or is there something else besides split(), I should be looking at?
Try this,
s.split(/\.(?![^\[]*\])/)
I got this result,
2.3.2 :061 > s.split(/\.(?![^\[]*\])/)
=> ["articles[zone.id=1]", "comments[user.status=active]", "user"]
You can also test it here:
https://rubular.com/r/LaxEFQZJ0ygA3j
I assume the problem is to split on periods that are not within matching brackets.
Here is a non-regex solution that works with any number of nested brackets. I've assumed the brackets are all matched, but it would not be difficult to check that.
def split_it(s)
left_brackets = 0
s.each_char.with_object(['']) do |c,a|
if c == '.' && left_brackets.zero?
a << '' unless a.last.empty?
else
case c
when ']' then left_brackets -= 1
when '[' then left_brackets += 1
end
a.last << c
end
end.tap { |a| a.pop if a.last.empty? }
end
split_it '.articles[zone.id=[user.loc=1]].comments[user.status=active].user'
#=> ["articles[zone.id=[user.loc=1]]", "comments[user.status=active]", "user"]
I'm using the function replace in a freemaker template like this:
example?replace("<|>|_|!", ' ', 'r')
The character '<' is not replaced. It can work with this ( I can't explain it=:
example?replace('<', ' ')
Any help ?
Thank you :)
I used input() in my program, to convert the input into a list type, but I read that I should use raw_input() instead.
I'm trying to let the user input vectors in the form (4,4),(2,5),(1,6)
Using input() it worked.
I used:
vectors = list(input('enter vectors\n'))
after changing it to raw_input() I get a list with all elements in the str
'(' '4' ',' '4' ')' ',' '(' '2' ',' '5' ')' ',' '(' '1' ',' '6' ')'
How can I get the input converted to a list I would get as if I used input()?
Should I just go back to using input() instead?
Assuming that you input your data in form 1,2 this could do your work:
vectors = []
print ('enter vectors')
while True:
a = raw_input('')
if a == 'q':
break
else:
vectors.append(tuple(int(c) for c in a.split(',')))
print vectors
You were right to switch to raw_input over input. As explained in this question, input takes the raw_input and than performs eval, which is generally bad practice and insecure if coming from an untrusted source.
To achieve what you desire while using raw_input, you can use the ast module
import ast
vector = raw_input('Enter Vector:')
vector = list(ast.literal_eval(vector))
print vector
>>> [(4, 4), (2, 5), (1, 6)]
How to write a regular expression for a text field which accepts all characters except a comma (,) and do not accept a white space at both the ends? I have tried
[^,][\B ]
but no use
like 'product generic no' instead of 'product,generic,no' or ' product generic no '
I suggest a solution without regular expression. As you said you're using JS so the function is in JavaScript:
function isItInvalid(str) {
var last = str.length - 1;
return (last < 2 ||
str[0] == ' ' ||
str[last] == ' ' ||
str.indexOf(',') != -1);
}
EDIT: Just made it a bit more readable. It also checks if the string is at least 3 chars.
Something like below:
/^\S[^,]*\S$/
Using a Perl regular expression
/^\S[^,]*\S$/
This should work from 2 characters up, but fails in the edge case where the string has only one non-comma character. To cover that too:
/^((\S[^,]*\S)|([^\s,]))$/
I have a char List is Scala where I want to remove all chars that are not parentheses. The problem is I only seem to be able to do this for one character, eg:
var parens = chars.filter(_ == '(')
If I try this:
var parens = chars.filter(_ == '(').filter(_ == ')')
..I get nothing since I am filtering it once, then a second time which removes everything. How can I filter a character List (not a string list) for multiple chars?
If you need/want a functional solution then try this:
val givenList = List('(', '[', ']', '}', ')')
val acceptedChars = List('(', ')')
givenList filter acceptedChars.contains // or givenList.filter(acceptedChars.contains)
Now you can add whatever chars you like to the seconds list on which you wanna filter the given list without changing filter call. If you want to leave chars that are not in the acceptedList just change to filterNot. Another advantage of this aproach, is that you do not need to write big lambda functions combining all the chars on which you wanna filter like: x => x == '(' || x == ')' || etc.
Update
Like senia proposed in the comment you can also use shorter version with Set just change function acceptedChars.contains with a Set of given chars:
givenList.filter(Set('(', ')'))
This will remove all characters that are not parentheses:
val parens = chars.filter(c=>c=='('||c==')')
The following is what I tested in scala console:
scala> val chars = List('a', 'b', '(', 'c', ')', ')')
chars: List[Char] = List(a, b, (, c, ), ))
scala> val parens = chars.filter(c=>c=='('||c==')')
parens: List[Char] = List((, ), ))
The reason that your code removes everything is that... the first filter (chars.filter(_ == '(')) removes all the characters that are not (, which means only ( remains. Applying filter(_ == ')') to this result returns empty list.