将列表列表与 python 中的列表进行比较

Comparing a list of lists to a list in python

我正在尝试将列表列表与 JES 中的简单列表进行比较 这是我试图比较的数据样本

list1 = [(1, 'abc'), (5, 'no'), (5, 'not'), (10, 'which')]
 list2 = ['not', 'which', 'abc']

基本上我正在做的是将一组单词及其频率 (list1) 与一组不同的单词 (list2) 进行比较,如果列表 2 与列表 1 匹配,则创建一个包含相同单词和列表 1 中的频率 这是下面列表 3 输出的示例

list3 = [(5, 'not'), (10, 'which'), (1, 'abc')]

这是使用缺少 python 的一些完整功能的 JES,所以我假设我只能用 for 循环或类似的

来回答这个问题

这是我目前尝试过的,还有其他一些组合

list3 = []
    for x in keywords:
     for y in frequencyList:
        if x == y[1]:
            list3.append(y) 

感谢您的帮助

在python我会做:

list3 = [x for x in list1 if x[1] in list2]

不过不确定 JES

您还可以将过滤器与 lambda 函数一起使用,使事情更加 pythonic!

>>> list1 = [(1, 'abc'), (5, 'no'), (5, 'not'), (10, 'which')]
>>> list2 = ['not', 'which', 'abc']
>>> filter(lambda x:x[1] in list2,list1)
[(1, 'abc'), (5, 'not'), (10, 'which')]