【问题标题】:Finding the intersection of nested lists in Python? [duplicate]在 Python 中查找嵌套列表的交集? [复制]
【发布时间】:2018-08-08 11:29:31
【问题描述】:
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】:

    假设顺序无关紧要,你可以使用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]每个令牌的发布列表。发布列表是嵌套字典的列表(例如 [{0 : 0.68426, 1: 0.26423}, {2: 0.6842332, 0: 0.9823}]。我正在尝试使用键找到这些嵌套字典的交集
    猜你喜欢
    • 2010-10-13
    • 1970-01-01
    • 2011-02-02
    • 2012-04-13
    • 1970-01-01
    相关资源
    最近更新 更多