Python 官方文档:入门教程 => 点击学习
问题内容 有人可以解释一下 python (v3.5) 字典是如何排序的吗? data = {"john": 23, "rick": 33, "mary": 63, "ron": 23
有人可以解释一下 python (v3.5) 字典是如何排序的吗?
data = {"john": 23, "rick": 33, "mary": 63, "ron": 23, "joel": 51}
for key in data:
print("your name is " + key + ", and you are also " + str(data[key]) + " years old.")
实际输出:
your name is rick, and you are also 33 years old.
your name is ron, and you are also 23 years old.
your name is mary, and you are also 63 years old.
your name is john, and you are also 23 years old.
your name is joel, and you are also 51 years old.
预期输出(字典的顺序):
Your name is John, and you are also 23 years old.
Your name is Rick, and you are also 33 years old.
Your name is Mary, and you are also 63 years old.
Your name is Ron, and you are also 23 years old.
Your name is Joel, and you are also 51 years old.
这取决于您使用的 Python 版本。
字典使用底层哈希函数的排序。多种类型具有加盐哈希,因此这意味着您在每次调用时获得不同的顺序。
字典是按插入顺序排列的,即字典会记住插入项目的顺序。来自文档:
Guido van Rossum 在 中宣布从 Python 3.7 开始所有 Python 实现都必须保留插入顺序。
以上就是理解Python字典的顺序的详细内容,更多请关注编程网其它相关文章!
--结束END--
本文标题: 理解Python字典的顺序
本文链接: https://lsjlt.com/news/562820.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0