Reputation: 11857
I have this list:
dc = ["hello", "world"]
And this other:
lines = ["This is", "an example of hello", "line in the world of strings", "Testing"]
I wish to find those elements on dc which are part of any element of lines...
I may loop lines as:
for line in lines:
# what to do here?
But I don't know how to find exactly that the "hello" element in dc is found in the "an example of hello" elemento of lines or that "world" in dc is found in the "line in the world of strings" in lines...
Maybe I shouldn't loop through lines?
Upvotes: 1
Views: 329
Reputation: 137460
set
's featuresFirst get all the words from the lines you have. Get them as a set for saving space and getting some useful features (see later). Then use &
operation on the above and set created from the words you are looking for. The solution could be a one line:
>>> set(dc) & set(sum(map(str.split, lines), []))
set(['world', 'hello'])
If you want the result as a list, just convert it to list like that:
>>> list(set(dc) & set(sum(map(str.split, lines), [])))
['world', 'hello']
Upvotes: 1
Reputation: 4723
>>> dc = ["hello", "world", "foo"]
>>> lines = ["This is", "an example of hello", "line in the world of strings", "Testing"]
>>> [word for word in dc if any(word in line for line in lines)]
['hello', 'world']
Upvotes: 6