I know that the and
and or
expressions exist in python, but is there any and/or
expression? Or some way to combine them in order to produce the same effect as a and/or
expression?
my code looks something like this:
if input=="a":
if "a" or "á" or "à" or "ã" or "â" in someList:
someList.remove("a") or someList.remove("á") or someList.remove("à") or someList.remove("ã") or someList.remove("â")
with this, I mean that if the user inputs "a" and any type of "a" is included in a previously defined list, can I have all the types of "a" removed from a given list?
python tells me that there is a problem in:
someList.remove("a") or someList.remove("á") or someList.remove("à") or someList.remove("ã") or someList.remove("â")
he tells me: ValueError: list.remove(x): x not in list
if input == 'a':
for char in 'abc':
if char in some_list:
some_list.remove(char)
For the updated question, you can replace what you want with something like:
someList = filter(lambda x: x not in ("a", "á", "à", "ã", "â"), someList)
filter
evaluates every element of the list by passing it to the lambda provided. In this lambda we check if the element is not one of the characters provided, because these should stay in the list.
Alternatively, if the items in someList
should be unique, you can make someList
a set and do something like this:
someList = list(set(someList)-set(("a", "á", "à", "ã", "â")))
This essentially takes the difference between the sets, which does what you want, but also makes sure every element occurs only once, which is different from a list. Note you could store someList
as a set from the beginning in this case, it will optimize things a bit.
Are you looking for...
a if b else c
Or perhaps you misunderstand Python's or
? True or True
is True
.
x and y
returns true if both x
and y
are true
.
x or y
returns if either one is true
.
From this we can conclude that or
contains and
within itself unless you mean xOR
(or except if and
is true)
or
is not exclusive (e.g. xor
) so or
is the same thing as and/or
.
Try this solution:
for m in ["a", "á", "à", "ã", "â"]:
try:
somelist.remove(m)
except:
pass
Just for your information. and
and or
operators are also using to return values. It is useful when you need to assign value to variable but you have some pre-requirements
operator or
returns first not null value
#init values
a,b,c,d = (1,2,3,None)
print(d or a or b or c)
#output value of a - 1
print(b or a or c or d)
#output value of b - 2
Operator and
returns last value in the sequence if any of the members don't have None
value or if they have at least one None value we get None
print(a and d and b and c)
#output: None
print(a or b or c)
#output value of c - 3
Source: Stackoverflow.com