
有人可以解释一下 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 版本。
立即学习“Python免费学习笔记(深入)”;
酷纬企业网站管理系统Kuwebs是酷纬信息开发的为企业网站提供解决方案而开发的营销型网站系统。在线留言模块、常见问题模块、友情链接模块。前台采用DIV+CSS,遵循SEO标准。 1.支持中文、英文两种版本,后台可以在不同的环境下编辑中英文。 3.程序和界面分离,提供通用的PHP标准语法字段供前台调用,可以为不同的页面设置不同的风格。 5.支持google地图生成、自定义标题、自定义关键词、自定义描
1
字典使用底层哈希函数的排序。多种类型具有加盐哈希,因此这意味着您在每次调用时获得不同的顺序。
字典是按插入顺序排列的,即字典会记住插入项目的顺序。来自文档:
Guido van Rossum 在 中宣布从 Python 3.7 开始所有 Python 实现都必须保留插入顺序。
以上就是理解Python字典的顺序的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号