关于python:发现一个秘密既python36之后字典竟然变成了有序集合我再次验证了一下

1次阅读

共计 970 个字符,预计需要花费 3 分钟才能阅读完成。

高版本我始终应用的是 3.8 的版本,我先用 python3.8 的版本来测试查看是不是会产生有序的字 ……

【浏览全文】

test_dict = {'o': 1,'p': 2,'q': 3,'r': 4,'s': 5,'t': 6}

应用 ksys() 函数验证字典的键是否有序

print(test_dict.keys())

# dict_keys(['o', 'p', 'q', 'r', 's', 't'])
# Process finished with exit code 0

遍历字典再次验证

for key,value in test_dict.items():
    print(key,value)

# dict_keys(['o', 'p', 'q', 'r', 's', 't'])
# o 1
# p 2
# q 3
# r 4
# s 5
# t 6

发现 python3.8 版本的字典汇合真的变成有序字典了。最初,找个 3.6 以下的版本再来验证一番,应用同样的数据来进行验证

# Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] on win32
# Type "help", "copyright", "credits" or "license" for more information.
# >>> test_dict = {'o': 1,'p': 2,'q': 3,'r': 4,'s': 5,'t': 6}
# >>> print(test_dict.keys())
# ['o', 'q', 'p', 's', 'r', 't']
# >>>

首先 keys() 函数遍历的键就是无序的

# >>> for key,value in test_dict.items():
# ...     print(key,value)
# ...
# ('o', 1)
# ('q', 3)
# ('p', 2)
# ('s', 5)
# ('r', 4)
# ('t', 6)

最初,遍历的键值都是无序的。明天就到这里了,小编才加完班该回家了!

【往期精彩】

这么多的内置函数能记住吗?对 python 的 68 个内置函数分类总结!

必须要会的文件操作对象 File,python 文件读写操作利器!

你不晓得的 CS 模式的过程管理工具,状态监测、我的项目启停高深莫测!

如何将一个 python 利用以 docker 镜像的形式来运行?

python 超赞插件 you-get,执行一行命令即可下载、命令行下载工具举荐!

正文完
 0