반응형
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
반응형
'source' 카테고리의 다른 글
pgsql 포트 변경 (0) | 2023.05.29 |
---|---|
창 양식 텍스트 상자에 새 줄을 추가하려면 어떻게 합니까? (0) | 2023.05.29 |
LINQ를 사용하여 최소 또는 최대 속성 값을 가진 개체를 선택하는 방법 (0) | 2023.05.29 |
Excel VBA에서 워크시트 생성 및 이름 지정 (0) | 2023.05.29 |
.NET 서비스 버스 대기열과 Azure 대기열 서비스 중에서 선택 (0) | 2023.05.29 |