Python3确定两个字典是否相等 [英] Python3 Determine if two dictionaries are equal
本文介绍了Python3确定两个字典是否相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这似乎微不足道,但我找不到确定两个字典是否相等的内置或简单方法。
This seems trivial, but I cannot find a built-in or simple way to determine if two dictionaries are equal.
我想要的是:
a = {'foo': 1, 'bar': 2}
b = {'foo': 1, 'bar': 2}
c = {'bar': 2, 'foo': 1}
d = {'foo': 2, 'bar': 1}
e = {'foo': 1, 'bar': 2, 'baz':3}
f = {'foo': 1}
equal(a, b) # True
equal(a, c) # True - order does not matter
equal(a, d) # False - values do not match
equal(a, e) # False - e has additional elements
equal(a, f) # False - a has additional elements
我可以编写一个短循环脚本,但我无法想象
I could make a short looping script, but I cannot imagine that mine is such a unique use case.
推荐答案
==
可行
a = dict(one=1, two=2, three=3)
b = {'one': 1, 'two': 2, 'three': 3}
c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
d = dict([('two', 2), ('one', 1), ('three', 3)])
e = dict({'three': 3, 'one': 1, 'two': 2})
a == b == c == d == e
True
我希望以上示例对您有所帮助。
I hope the above example helps you.
这篇关于Python3确定两个字典是否相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文