I have 2 lists. One is a list of words and their frequencies and the other is a list of words.
a = [('country',3478), ('island',2900),('river',5)]
b = ['river','mountain','bank']
There are thousands of entries in a but only hundreds in b.
How can I subset list a so that i return:
c=[('river',5)]
For loops would take too long given the number of entries and i imagine list comprehension is the solution but cannot get it right.
My main goal is to then create a wordcloud with my final list. Any help would be appreciated
**Edited because I made a mistake as pointed out by some commenters. I want to return
c=[('river',5)]
instead of
c=['river',5]
as i originally wrote. Apologies and thanks for pointing it out
You can try this:
Output:
I assume you actually want:
You better first construct a set of values in
b
:then you can use list comprehension:
That being said, if you want to lookup the frequency of a word, I advice you not to construct a list of tuples, but a dictionary. You can do this with dictionary comprehension: