-->

How to subset an item:value list using another lis

2019-07-03 05:30发布

问题:

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

回答1:

I assume you actually want:

c = [('river',5)]  # a list with one tuple

You better first construct a set of values in b:

bd = set(b)

then you can use list comprehension:

c = [(x,y) for x,y in a if x in bd]

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:

c = {x: y for x,y in a if x in bd}  # dictionary variant


回答2:

You can try this:

a = [('country',3478), ('island',2900),('river',5)] 
b = ['river','mountain','bank'] 
final_data = list([i for i in a if i[0] in b][0])

Output:

['river', 5]