加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

是否按顺序评估Python字典文字?

发布时间:2020-12-20 12:07:47 所属栏目:Python 来源:网络整理
导读:参见英文答案 Will a Python dict literal be evaluated in the order it is written?????????????????????????????????????3个 当我跑这个 import itertoolsbar = itertools.count(1).nextprint {'a': bar(),'b': bar(),'c': bar()} 在CPython上,我得到{‘a
参见英文答案 > Will a Python dict literal be evaluated in the order it is written?????????????????????????????????????3个
当我跑这个

import itertools

bar = itertools.count(1).next

print {'a': bar(),'b': bar(),'c': bar()}

在CPython上,我得到{‘a’:0,’b’:1,’c’:2}.这在实施中是否一致和/或在任何地方都有记录? a,b和c是否会被无序分配值?

解决方法

从 Python Docs(强调我的):

If a comma-separated sequence of key/datum pairs is given,they are evaluated from left to right to define the entries of the dictionary: each key object is used as a key into the dictionary to store the corresponding datum….

因此,将始终按顺序评估对.但不要混淆元素的顺序.

(编辑:李大同)

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

    推荐文章
      热点阅读