如何使用仅包含项目的另一个列表来对item:value列表进行子集化? [英] How to subset an item:value list using another list with just items?

查看:67
本文介绍了如何使用仅包含项目的另一个列表来对item:value列表进行子集化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

a中有成千上万的条目,而b中仅有数百.

There are thousands of entries in a but only hundreds in b.

如何子集列出a以便返回:

How can I subset list a so that i return:

c=[('river',5)]

给定条目数量,for循环将花费很长时间,我想列表理解是解决方案,但无法正确解决.

For loops would take too long given the number of entries and i imagine list comprehension is the solution but cannot get it right.

我的主要目标是使用最终列表创建wordcloud.任何帮助将不胜感激

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

代替

c=['river',5]

就像我最初写的那样.致歉,并感谢您指出

as i originally wrote. Apologies and thanks for pointing it out

推荐答案

我认为您实际上是想要的:

I assume you actually want:

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

最好先在b中构造一组值:

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

这篇关于如何使用仅包含项目的另一个列表来对item:value列表进行子集化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆