问题
This question arises from this answer where one user uses
d.keys()
andd.values()
separately to initialise a dataframe.
It's common knowledge that dictionaries in python versions under 3.6 are not ordered.
Consider a generic dictionary of the form:
d = {k1 : v1, k2 : v2, k3 : v3}
Where the keys k*
are any hashable objects, and the values v*
being any object. Of course, order cannot be guaranteed, but what about the order of d.keys()
and d.values()
?
Python 2.x
Both d.keys()
and d.values()
return lists. Say, .keys()
returns d
's keys in the order [k2, k1, k3]
. Is it now always guaranteed that d.values()
returns the same relative ordering as [v2, v1, v3]
? Furthermore, does the ordering remain the same no matter how many times these functions are called?
Python 3.x (<3.6)
I'm not 100% sure, but I believe that .keys
and .values
do not guarantee any ordering at all here because they are set-like structures, thus having no order by definition and enabling you to perform set-like operations on them. But I'd still be interested to know if there is any sort of relative ordering between the two calls in this instance. I'm guessing not. I'd appreciate if someone could affirm or correct me.
回答1:
The general rules:
- Before talking about what is guaranteed and what isn't, even if some ordering seems to be "guaranteed", it isn't. You should not rely on it. It is considered bad practice, and could lead to nasty bugs.
d.keys()
,d.values()
, andd.items()
all return the elements in a respective order. The order should be treated as arbitrary (no assumptions should be made about it). (docs)- consecutive calls to
d.keys()
,d.values()
, andd.items()
are "stable", in the sense they are guaranteed to preserve the order of previous calls (assuming no insertion/deletion happens between the calls). - Since CPython's V3.6, dict has been reimplemented, and it now preserves insertion order. This was not the goal of the change, but a side effect, and it is NOT part of the python spec, only a detail of the CPython implementation. See point #1 above: relying on this is bad practice and should not be done. Anyways, you should avoid writing CPython-specific code.
- In Python2, order is deterministic (i.e. creating a dict twice in the same way will result with the same order). In Python <3.6, it is no longer deterministic, so you can't rely on that either (I'm not sure if this non-determinism is part of the spec or just a CPython implementation detail).
EDIT: added point #5, thanks to @AndyHayden's comment.
来源:https://stackoverflow.com/questions/47172349/what-ordering-does-dict-keys-and-dict-values-guarantee