【问题标题】:Pythonic way to compare two lists and print the unmatched items?比较两个列表并打印不匹配项的 Pythonic 方式?
【发布时间】:2010-08-20 09:51:19
【问题描述】:

我有两个 Python 字典列表,entries9 和 entries10。我想比较这些项目并将联合项目写入一个名为joint_items 的新列表。我还想将不匹配的项目保存到两个新列表中,unmatched_items_9 和 unmatched_items_10。

这是我的代码。获取joint_items 和unmatched_items_9(在外部列表中)非常容易:但是我如何获取unmatched_items_10(在内部列表中)?

for counter, entry1 in enumerate(entries9):
    match_found = False
    for counter2,entry2 in enumerate(entries10):
        if match_found:
            continue
        if entry1[a]==entry2[a] and entry1[b]==entry2[b]: # the dictionaries only have some keys in common, but we care about a and b
            match_found = True
            joint_item = entry1
            joint_items.append(joint_item)
            #entries10.remove(entry2) # Tried this originally, but realised it messes with the original list object!
    if match_found:
        continue
    else: 
        unmatched_items_9.append(entry1)

性能并不是真正的问题,因为它是一次性脚本。

【问题讨论】:

  • 我认为您可以在内部循环中将 if match_found: continue 替换为 if match_found: break(末尾)。
  • 好点@SiggyF!是的,我可以。

标签: python list


【解决方案1】:

相当于你目前正在做的事情,但反过来,是:

unmatched_items_10 = [d for d in entries10 if d not in entries9]

虽然比您的编码方式更简洁,但它存在相同的性能问题:它所花费的时间与每个列表中的项目数成正比。如果您感兴趣的长度大约是 9 或 10(这些数字似乎表明),没问题。

但是对于相当长的列表,您可以通过对列表进行排序并“并行”“逐步”通过它们来获得更好的性能,可以这么说(时间与N log N 成正比,其中N 是较长列表的长度)。如果即使这种更高级的方法也不足以让您获得所需的性能,那么还有其他可能性(越来越复杂;-)。我不会建议非常复杂的东西,除非你表明你确实需要它来获得良好的性能(在这种情况下,请提及每个列表的典型长度和字典的典型内容他们的项目,因为这些“细节”当然是挑选算法的关键考虑因素,是速度和简单性之间的良好折衷)。

编辑:OP 编辑​​了他的 Q 以显示他关心的内容,对于两个列表中的任意两个字典 d1 和 d2,不是 d1 == d2(其中是in 操作员检查的内容),而是d1[a]==d2[a] and d1[b]==d2[b]。在这种情况下不能使用 in 运算符(好吧,不是没有一些时髦的包装,但这是一个在可行的情况下最好避免的复杂情况;-),但 all 内置函数可以轻松替换它:

unmatched_items_10 = [d for d in entries10
                      if all(d[a]!=d1[a] or d[b]!=d2[b] for d2 in entries9)]

我已经改变了逻辑(到!= 和or,每个De Morgan's laws),因为我们想要不匹配的字典。但是,如果您愿意:

unmatched_items_10 = [d for d in entries10
                      if not any(d[a]==d1[a] and d[b]==d2[b] for d2 in entries9)]

就我个人而言,出于文体原因,我不喜欢if not any 和if not all,但数学是无可挑剔的(根据维基百科页面将Extensions 称为德摩根定律,因为any 是存在主义的量词和all 一个通用量词,可以这么说;-)。性能应该差不多(但是,OP 确实在评论中澄清说,性能对他们来说在这项任务中并不是很重要)。

【讨论】:

  • 感谢您的详细解答。性能不是问题——它是清理一些数据的一次性脚本,不管需要多长时间。不幸的是,'d not in entries9' 不起作用,因为匹配条件更复杂 - 我必须比较某些字段。它更像是“如果 d[a]==entries9_item[a] 和 d[b]==entries9_item[b]”。我将更新问题以使其更清楚。
  • @AP257,您会首先提一下,您知道的,您知道-- 相等检查显然是特殊情况,这就是您使用的;-)。无论如何,编辑我的答案以显示代码如何更改。
  • 对不起。谢谢你 - 非常巧妙地使用 all() 和 any()。要获得joint_items列表,你认为我应该简单地做“joint_items = [d for d in entries10 if all(d[a]==d1[a] or d[b]==d2[b] for d2 in entries9 )]"?这似乎是重复的,但可能比弄乱原始对象更安全。
  • @AP257,更安全,是的,虽然你想要any,而不是all。虽然 三个 循环开始拉伸它,但没有一个干净的方法可以用一个循环来完成它,因此潜在的性能提升很小。如果每个d 的d[a] 和d[b] 都是可散列的,那么当然有更快的方法(但你确实说你不太关心这里的性能,所以我只做三个循环)。
【解决方案2】:

Python stdlib 有一个类 difflib.SequenceMatcher,看起来它可以做你想做的事,虽然我不知道如何使用它!

【讨论】:

    【解决方案3】:

    您可以考虑使用sets 及其相关方法,例如intersection。但是,您需要将字典转换为不可变数据,以便可以将它们存储在 set(例如 strings)中。像这样的东西有用吗?

    a = set(str(x) for x in entries9)
    b = set(str(x) for x in entries10)  
    
    # You'll have to change the above lines if you only care about _some_ of the keys
    
    joint_items = a.union(b)
    unmatched_items = a - b
    
    # Now you can turn them back into dicts:
    joint_items     = [eval(i) for i in joint_items]
    unmatched_items = [eval(i) for i in unmatched_items]
    

    【讨论】:

    • 如果可能的话,我会使用dict.items 和dict 而不是str 和eval。
    猜你喜欢
    • 1970-01-01
    • 2016-10-23
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 2012-09-05
    相关资源
    最近更新 更多