读童话的狼

2020-11-23   阅读量: 1432

Python

python中的dict是有序的,还是无序的?

扫码加入数据分析学习群

image.png

图片截取自 Python 核心开发者 Raymond Hettinger 在 Youtube 上的一个视频 Modern Dictionaries by Raymond Hettinger 。Raymond 也是 Python 3.6 中 new dict 的提议者和发起者。

可以看到,Python 3.6 之前,字典都是无序存储的,但到了 Python 3.6,就变成了有序的

如果想要在代码中保证字典有序,尤其是涉及到其他版本的 Python 实现的时候,还是得用 OrderedDict。


from collections import OrderedDict

xxoo = OrderedDict()

xxoo['a'] = 1

xxoo['b'] = 2

xxoo['c'] = 3


for i,j in xxoo.items():

print ('{} --> {}'.format(i,j))


40.8018 4 0 关注作者 收藏

评论(0)


暂无数据

推荐课程

推荐帖子