在Python中查找嵌套列表的交集? [重复]

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

这个问题在这里已有答案:

def query_RR(postings, qtext): 
words = tokenize(qtext) 
allpostings = [postings[w] for w in words]
for a in allpostings: 
print a.keys()

这是查询[0,2,3,4,6] [1,4,5] [0,2,4] [4,5]的结果

该查询采用用户输入的术语('qtext'),标记化并生成每个令牌的发布列表。

发布列表是嵌套字典的列表(例如[{0:0.68426,1:0.26423},{2:0.6842332,0:0.9823}]。我试图使用键找到这些嵌套字典的交集

python list intersection
1个回答
4
投票

假设订单无关紧要,您可以使用set.intersection()

>>> lst = [[0, 2, 3, 4, 6], [1, 4, 5], [0, 2, 4], [4, 5]]
>>> set.intersection(*map(set,lst))
{4}
>>> set(lst[0]).intersection(*lst[1:])
{4}
© www.soinside.com 2019 - 2024. All rights reserved.