I have two python lists:
a = [('when', 3), ('why', 4), ('throw', 9), ('send', 15), ('you', 1)]
b = ['the', 'when', 'send', 'we', 'us']
I need to filter out all the elements from a that are similar to those in b. Like in this case, I should get:
c = [('why', 4), ('throw', 9), ('you', 1)]
What should be the most effective way?
解决方案
A list comprehension will work.
a = [('when', 3), ('why', 4), ('throw', 9), ('send', 15), ('you', 1)]
b = ['the', 'when', 'send', 'we', 'us']
filtered = [i for i in a if not i[0] in b]
>>>print(filtered)
[('why', 4), ('throw', 9), ('you', 1)]