Do dictionaries in Python have a single meaning in the editor?
It was suggested in this question that invoking the correspondence in a dictionary would be a good way to store it in another dictionary. This will depend on whether the replica is the same regardless of how the keys are ordered. Is this the case?
PS. the most elegant solution to the original problem was actually using frozenset
+2
source to share
3 answers
No, the order in which keys are added to the dictionary can affect the internal data structure. When two elements have the same hash value and end up in the same bucket, then the order they add to the dictionary matters.
>>> (1).__hash__()
1
>>> (1 << 32).__hash__()
1
>>> repr({1: 'one', 1 << 32: 'not one'})
"{1: 'one', 4294967296L: 'not one'}"
>>> repr({1 << 32: 'not one', 1: 'one'})
"{4294967296L: 'not one', 1: 'one'}"
+7
source to share