I have a list of strings and want to use various different operators to to pick out the string that satisfies my conditions.
The example code below gives me the answer I'm looking for:
strings = ['abc','bcd','cde','dea', 'eab']
for string in strings:
if 'a' in string and not 'b' in string and ('ea' not in string or 'd' in string):
print(string)
>> dea
However, this code requires me to write out 'in string' multiple times, which looks messy and takes time when referencing a number of conditions.
I want to know if there's a way to condense the code down to show that everything operator is searching -in string-
I would imagine the syntax looks something like:
strings = ['abc','bcd','cde','dea', 'eab']
for string in strings:
if ('a' and not 'b' and ('ea' not or 'd')) in string:
print(string)
but when I try this I get a SyntaxError.
Is there a pythonic way to condense the code?
Aucun commentaire:
Enregistrer un commentaire