在Python中找到嵌套列表的交集? [英] Finding the intersection of nested lists in Python?

查看:113
本文介绍了在Python中找到嵌套列表的交集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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]的结果

And this was the result of the query [0, 2, 3, 4, 6] [1, 4, 5] [0, 2, 4] [4, 5]

该查询使用用户输入的术语("qtext"),标记化并为每个标记生成发布列表.

The query is taking a user inputted term ('qtext'), tokenizing and generating a postings list for each token.

发布列表是嵌套词典的列表(例如[{0:0.68426,1:0.26423},{2:0.6842332,0:0.9823}].我正在尝试使用键来查找这些嵌套词典的交集.

The posting list is a list of nested dictionaries (e.g. [{0 : 0.68426, 1: 0.26423}, {2: 0.6842332, 0: 0.9823}]. I am attempting to find the intersection for these nested dictionaries using the keys

推荐答案

假设顺序无关紧要,则可以使用set.intersection():

Assuming the order does not matter, you could use 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}

这篇关于在Python中找到嵌套列表的交集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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