如何使用另一个仅包含项目的列表来对 item:value 列表进行子集化?

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

我有 2 个列表。一个是单词及其频率的列表,另一个是单词列表。

a = [('country',3478), ('island',2900),('river',5)] 
b = ['river','mountain','bank'] 

a 中有数千个条目,但 b 中只有数百个条目。

如何对列表 a 进行子集化,以便 return:

c=[('river',5)]

考虑到条目的数量,For 循环会花费太长时间,我认为列表理解是解决方案,但无法正确解决。

我的主要目标是用我的最终列表创建一个词云。任何帮助将不胜感激


**编辑是因为我犯了一些评论者指出的错误。我要return

c=[('river',5)]

而不是

c=['river',5]

正如我最初写的那样。抱歉并感谢您指出这一点

我假设你真的想要:

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

你最好先在b中构建一组值:

bd = set(b)

那么你可以使用列表理解:

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

话虽这么说,如果你想查询一个词的频率,我建议你不要构建一个元组列表,而是一个字典。您可以使用 dictionary comprehension:

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

你可以试试这个:

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])

输出:

['river', 5]