为了账号安全,请及时绑定邮箱和手机立即绑定

为什么字典排序是非确定性的?

为什么字典排序是非确定性的?

12345678_0001 2019-07-23 15:08:05
为什么字典排序是非确定性的?我最近从Python 2.7切换到Python 3.3,似乎在Python 2中,字典键的排序是任意的但是一致的,在Python 3中,用例如获得的字典的键的排序vars()看起来是非确定性的。如果我跑:class Test(object): passparameters = vars(Test)print(list(parameters.keys()))在Python 2.7和Python 3.3中,然后:Python 2.7一直给我['__dict__', '__module__', '__weakref__', '__doc__']使用Python 3.3,我可以得到任何随机顺序 - 例如:['__weakref__', '__module__', '__qualname__', '__doc__', '__dict__']['__doc__', '__dict__', '__qualname__', '__module__', '__weakref__']['__dict__', '__module__', '__qualname__', '__weakref__', '__doc__']['__weakref__', '__doc__', '__qualname__', '__dict__', '__module__']这种非决定论来自哪里?为什么会这样list({str(i): i for i in range(10)}.keys())......在跑步之间保持一致,总是给予['3', '2', '1', '0', '7', '6', '5', '4', '9', '8']......?
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 391 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号