source

Python3 사전 두 개가 동일한지 확인

nicesource 2023. 5. 29. 11:00
반응형

Python3 사전 두 개가 동일한지 확인

이것은 사소한 것처럼 보이지만 두 개의 사전이 동일한지 확인할 수 있는 기본 제공 또는 간단한 방법을 찾을 수 없습니다.

제가 원하는 것은:

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

저는 짧은 루프 스크립트를 만들 수 있지만, 제 것이 그렇게 독특한 사용 사례라고는 상상할 수 없습니다.

==작동하다

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

위의 예시가 당신에게 도움이 되길 바랍니다.

좋은 노인들==문이 작동합니다.

언급URL : https://stackoverflow.com/questions/53348959/python3-determine-if-two-dictionaries-are-equal

반응형