How to subset an item:value list using another list with just items?

こ雲淡風輕ζ 提交于 2019-12-10 19:37:53

问题


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]


来源:https://stackoverflow.com/questions/46794141/how-to-subset-an-itemvalue-list-using-another-list-with-just-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!