Skip to main content
 首页 » 编程设计

python之哪个 PEP 管理 dict.values() 的顺序

2025年05月04日9myhome

当您调用 dict.values() 时,返回项的顺序取决于键的 has 值。这似乎在所有版本的 cPython 中都非常一致,但是 dict simply states that the ordering is "arbitrary" 的 python 手册.

我记得在某处读到实际上有一个 PEP,它专门说明了 items() 和 values() 方法的预期顺序。

仅供引用,如果这种行为确实是我正在处理的类(class)的保证行为,我可以大大简化和加速我正在处理的类(class)。另一方面,如果这只是 cPython 的一个意外且未记录的功能,那么最好不要相信它。

请您参考如下方法:

来自 http://docs.python.org/library/stdtypes.html :

Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions.