How to subset an item:value list using another lis

2019-07-03 05:11发布

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

2条回答
Root(大扎)
2楼-- · 2019-07-03 05:39

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]
查看更多
霸刀☆藐视天下
3楼-- · 2019-07-03 05:55

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
查看更多
登录 后发表回答