combine if filter terms from list
python at mrabarnett.plus.com
Fri Apr 21 20:59:56 EDT 2017
On 2017-04-22 01:17, Rory Schramm wrote:
> I'm trying to use python list comprehensions to combine multiple terms for
> use by a for loop if condition.
> filters = [ 'one', 'two', 'three']
> for line in other_list:
> if ' and '.join([item for item in filters]) not in line:
> print line
> The list comp returns one and two and three and ..
> The problem I'm having is the for loop isn't filtering out the terms from
> the filter list. I suspect the problem is the if condition is treating the
> results for the list comprehension as a literal string and not part of the
> if condition itself. I'm not sure how to fix this though.
Correct. The join is returning 'one and two and three'. The condition is
true if that string isn't in line. (What is line? Is it a string? If
so, then line is one character of that string.)
> Any ideas on How to make this work?
If you want it to do this:
if 'one' not in line and 'two' not in line and 'three' not in
you can write:
if all(word not in line for word in filters):
if not any(word in line for word in filters):
More information about the Python-list