简体   繁体   English

比较两个二元组列表并返回匹配的二元组

[英]Compare two bigrams lists and return the matching bigram

Please suggest how to compare 2 bigrams lists and return the matching bigram only.请建议如何比较 2 个二元组列表并仅返回匹配的二元组。 From the below example lists, how to return the matching bigrams ['two', 'three'] .从下面的示例列表中,如何返回匹配的二元组['two', 'three']

bglist1 = 
[['one', 'two'],
 ['two', 'three'],
 ['three', 'four']]

bglist2 = 
[['one', 'six'],
 ['two', 'four'],
 ['two', 'three']]

You could just test for if the bigram is in the other list of bigrams.您可以只测试二元组是否在另一个二元组列表中。

out = list()

for x in bglist1:
    if x in bglist2:
        out.append(x)

This would give you a list of lists that are in both bglists.这将为您提供两个 bglist 中的列表列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM