微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

参见英文答案 > 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….

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐