【问题标题】:How to compare two OrderedDict dictionaries?如何比较两个 OrderedDict 字典?
【发布时间】:2022-05-19 05:29:39
【问题描述】:

如何比较两个 OrderedDict 字典?

我的结构如下:

dict_a = OrderedDict([(1,4), (2,5), (3,3), (4,5), (5,4), (6,4), (7,4), (8,3), (9,4)])

dict_b = OrderedDict([(1,4), (2,2), (3,1), (4,4), (5,6), (6,7), (7,4), (8,2), (9,5)])

for values in score_dict.items():
if values == course_dict.values():
    print 'match!'
else:
    print 'No match!'

它遍历并且两个列表都是有序的,所以它应该在 1 和 7 上匹配? 提前致谢!

【问题讨论】:

  • 感谢您的所有回答!让它与 Simeon 的解决方案一起使用。

标签: python


【解决方案1】:

您可以使用items() 和内置的zip() 函数:

for i, j in zip(dict_a.items(), dict_b.items()):
    if i == j:
        print(i)

输出:

(1, 4)
(7, 4)

【讨论】:

    【解决方案2】:

    如果您希望每个有序位置的相交元素都相同:

    >>> from collections import OrderedDict
    >>> dict_a = OrderedDict([(1,4), (2,5), (3,3), (4,5), (5,4), (6,4), (7,4), (8,3), (9,4)])
    >>> dict_b = OrderedDict([(1,4), (2,2), (3,1), (4,4), (5,6), (6,7), (7,4), (8,2), (9,5)])
    >>> [i1 for i1, i2 in zip(dict_a.iteritems(), dict_b.iteritems()) if i1 == i2]
    [(1, 4), (7, 4)]
    

    如果您不关心订购:

    >>> set(dict_a.items()).intersection(set(dict_b.items()))
    set([(7, 4), (1, 4)])
    

    【讨论】:

      【解决方案3】:
      >>> for x in dict_a.items():
          if x in dict_b.items():
              print(x)
      
      
      (1, 4)
      (7, 4)
      

      【讨论】:

      • 这些是字典。这里不需要 O(n**2) 解决方案。
      • 这不会保留排序 - 不确定这是否是 OP 想要的
      • @jterrace:是的。 items() 的结果是有序的。
      • 不,但它会匹配位置 i 中的项目 x,而该项目 x 不在另一个字典中的位置 i 中
      • @jterrace:字典的“项”是键值对。
      【解决方案4】:

      如果您发现它们相等,您可以遍历这些项目并打印它。

      In [10]: for a, b in zip(dict_a.items(), dict_b.items()):
         ....:     if a == b:
         ....:         print True
         ....:     else:
         ....:         print False
      

      【讨论】:

        【解决方案5】:

        假设您希望键和值都匹配,但不关心每个 OrderedDict 中的位置:

        for k, v in dict_a.items():
          if v == dict_b[k]:
            print '(%s, %s)' % (k, v)
        

        【讨论】:

          【解决方案6】:

          如果您想在 OrderedDict 中包含索引作为比较的一部分:

          >>> [a[0] for a, b in zip(dict_a.items(), dict_b.items()) if a == b]
          [1, 7]
          

          如果您想获取两个 OrderedDicts 中具有相同值的所有键,无论顺序如何:

          >>> [k for k, v in dict_a.items() if k in dict_b and dict_b[k] == v]
          [1, 7]
          

          【讨论】:

            【解决方案7】:

            您还可以将 OrderDict().items() 转换为集合并使用集合算术。了解基础知识后,集合算术非常强大且直观。

            
            In [39]: set_a = set(dict_a.items())
            
            In [40]: set_b = set(dict_b.items())
            
            In [41]: set_a.intersection(set_b) # items in a and b
            Out[41]: {(1, 4), (7, 4)}
            
            In [42]: dict(set_a.intersection(set_b)) # if you need to move back to a dict
            Out[42]: {7: 4, 1: 4}
            
            In [43]: set_a - set_b # stuff in set a but not in b
            Out[43]: {(2, 5), (3, 3), (4, 5), (5, 4), (6, 4), (8, 3), (9, 4)}
            
            In [44]: set_b - set_a # stuff in set b but not in a
            Out[44]: {(2, 2), (3, 1), (4, 4), (5, 6), (6, 7), (8, 2), (9, 5)}
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-03-21
              • 2017-12-09
              • 1970-01-01
              • 2017-08-21
              • 2016-10-22
              • 2012-05-10
              • 1970-01-01
              相关资源
              最近更新 更多