加入收藏 | 设为首页 | 会员中心 | 我要投稿 商洛站长网 (https://www.0914zz.com/)- AI应用、CDN、边缘计算、云计算、物联网!
当前位置: 首页 > 编程开发 > Python > 正文

python – OrderedDict:也是有序的值?

发布时间:2020-11-18 07:04:27 所属栏目:Python 来源:互联网
导读:参见英文答案 Python dictionary: are keys() and values() always the same order?8个 collections.OrderedDict文档将OrderedDict描述为 a dict that remembers the order that keys were

参见英文答案 > Python dictionary: are keys() and values() always the same order?8个
collections.OrderedDict文档将OrderedDict描述为

a dict that remembers the order that keys were first inserted

所以顺序

for k in dict:
  ...

for k in dict.keys():
  ...

是可以预测的.

但是,它没有说出价值观.如果我只需要按如下方式迭代值,结果是否也会遵循逐个插入的顺序?

for v in dict.values():
  ...

CPython中的一些快速测试表明情况就是如此,但这可能只是当前实现的coinicidental(我没有测试任何其他).

解决方法

是的,按键()和值()的列表按所有词组中的相应顺序排列,not just in ordered ones.(对于普通词典,顺序是任意的,但对于keys(),values()和items(),它是相同的任意顺序除非在此期间修改了字典.)

(编辑:商洛站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读