在 Python 中查找嵌套列表的交集?
Finding the intersection of nested lists in 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]
查询采用用户输入的术语 ('qtext'),对每个标记进行标记并生成一个帖子列表。
发布列表是嵌套词典的列表(例如 [{0 : 0.68426, 1: 0.26423}, {2: 0.6842332, 0: 0.9823}])。我试图找到这些嵌套词典的交集,使用钥匙
假设顺序无关紧要,您可以使用 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}
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}])。我试图找到这些嵌套词典的交集,使用钥匙
假设顺序无关紧要,您可以使用 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}