dict.keys()和dict.values()可以保证什么顺序? [英] What ordering does dict.keys() and dict.values() guarantee?

查看:237
本文介绍了dict.keys()和dict.values()可以保证什么顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题源自此 答案一个用户 分别使用d.keys()d.values()初始化数据帧.

This question arises from this answer where one user uses d.keys() and d.values() separately to initialise a dataframe.

众所周知,在3.6版以下的python版本中,字典不排序.

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}

其中键k*是任何可哈希对象,而值v*是任何对象.当然,不能保证顺序,但是d.keys()d.values()的顺序又如何呢?

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

d.keys()d.values()都返回列表.假设.keys()[k2, k1, k3]的顺序返回d的键.现在是否总是保证 d.values()返回与[v2, v1, v3]相同的相对顺序?而且,无论这些函数被调用多少次,其顺序都保持不变吗?

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)

我不确定100%,但是我相信.keys.values在这里根本不保证任何顺序,因为它们是类似集合的结构,因此按定义没有顺序,使您可以执行集合类似的操作.但是我仍然想知道在这种情况下两个调用之间是否存在任何相对排序. 我猜不会.如果有人可以肯定或纠正我,我将不胜感激.

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. 在讨论保证什么和不保证什么之前,即使似乎可以保证"某种订购,也不能.您不应该依赖它.这被认为是不良做法,并可能导致令人讨厌的错误.
  2. d.keys()d.values()d.items()均以各自的顺序返回元素.该顺序应视为任意顺序(不应对此作出任何假设). (文档)
  3. 连续调用d.keys()d.values()d.items()是稳定的",从某种意义上说,可以保证它们保持先前调用的顺序(假定在两次调用之间没有插入/删除操作).
  4. 自CPython V3.6起, dict已重新实现,现在可以保留插入的顺序.这不是改变的目的,而是副作用,它不是python规范的一部分,只是CPython实现的一个细节.请参阅上面的第1点:依靠这种做法是错误的做法,不应这样做.无论如何,您应该避免编写特定于CPython的代码.
  5. 在Python2中,顺序是确定性的(即,以相同的方式两次创建字典将以相同的顺序产生结果).在Python< 3.6中,它不再是确定性的,因此您也不能依赖它(我不确定这种不确定性是规范的一部分还是只是CPython实现细节).
  1. 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.
  2. d.keys(), d.values(), and d.items() all return the elements in a respective order. The order should be treated as arbitrary (no assumptions should be made about it). (docs)
  3. consecutive calls to d.keys(), d.values(), and d.items() are "stable", in the sense they are guaranteed to preserve the order of previous calls (assuming no insertion/deletion happens between the calls).
  4. 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.
  5. 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).

感谢@AndyHayden的评论,添加了第5点.

added point #5, thanks to @AndyHayden's comment.

这篇关于dict.keys()和dict.values()可以保证什么顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆