在Python中相交的二元组三元组

问题描述 投票:-1回答:1

我有一个二元组,我有一个三字母组,如何匹配python中三元组中的一个二元组?

我已经尝试过示例,但是由于我是新手,所以我不明白,非常感谢。

Bigram = [('red', 'car'), ('new', 'york'), ('table', 'window')]
Trigram = [('red', 'car','expensive,), ('new', 'york','city')]

结果:[('red', 'car'),('new', 'york')]

python nlp nltk
1个回答
3
投票

您可以使用集合通过将列表中的三元组转换为二元组来获得交集:

Bigram = [('red', 'car'), ('new', 'york'), ('table', 'window')]
Trigram = [('red', 'car','expensive'), ('new', 'york','city')]

set(Bigram).intersection( (a,b) for x,y,z in Trigram for a,b in ([x,y],[y,z]) )

# {('new', 'york'), ('red', 'car')}
© www.soinside.com 2019 - 2024. All rights reserved.