I think this is a simple question, so I'll just go straight to an example.
Given these two lists:
x = ['a', 'ab', 'abc', 'bc', 'c', 'ac']
y = ['a', 'b']
How do I write a list comprehension to filter list x in such a way that the result would be:
result = ['c']
I want a list comprehension that excludes any partial matches of the strings in y to the strings in x. For example, 'a' in y would match 'a', 'ab', 'abc', and 'ac' in x.
This comprehension only matches entire strings: result = [r for r in x if r not in y]
If this has already been asked I'll gladly accept a link to a previous answer. That said, I haven't found one on SO yet.
Let’s see another example where list comprehension is used on a string. LIst comprehension can identify the input if a string or list or tuple, etc., and work accordingly as it does for a string. We can also give conditionals to the list comprehensions and let the element add to the new list only if the condition is matched.
List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list. Based on a list of fruits, you want a new list, containing only the fruits with the letter "a" in the name. Without list comprehension you will have to write a for statement with a conditional test inside:
Given two lists of strings string and substr, write a Python program to filter out all the strings in string that contains string in substr. We can Use list comprehension along with in operator to check if the string in ‘substr’ is contained in ‘string’ or not.
Here, ‘i’ is mapped to the tuple, and for each value of ‘i’ in the tuple, the condition of ‘i<10 is checked for filtering. Then this mapping is converted to a list using the list () function and this list consist only of those values that are greater than 10.
Use all:
result = [r for r in x if all(z not in r for z in y)]
Or any:
result = [r for r in x if not any(z in r for z in y)]
This is a job for the any
built-in.
>>> x = ['a', 'ab', 'abc', 'bc', 'c', 'ac']
>>> y = ['a', 'b']
>>> [r for r in x if not any(s in r for s in y)]
['c']
s in r
does the partial match you want, for s in y
checks all elements of y
, and any
is true if there was any match. Then we just invert it.
This is quadratic, O(len(x) * len(y)
). If y
is long, it may be more efficient to synthesize a regexp:
>>> import re
>>> yy = re.compile("|".join(re.escape(s) for s in y))
>>> [r for r in x if not yy.search(r)]
['c']
which should be merely O(len(x) + len(y)
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With