为什么python这样排序我的字典? [英] Why is python ordering my dictionary like so?

查看:108
本文介绍了为什么python这样排序我的字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我有的字典

propertyList = {
    "id":           "int",
    "name":         "char(40)",

    "team":         "int",
    "realOwner":    "int",

    "x":            "int",
    "y":            "int",

    "description":  "char(255)",

    "port":         "bool",
    "secret":       "bool",
    "dead":         "bool",
    "nomadic":      "bool",

    "population":   "int",
    "slaves":       "int",
}

但是当我用\\\
.join(myDict)打印出来时,我得到这个

But when I print it out with "\n".join(myDict) I get this

name
nomadic
dead
port
realOwner
secret
slaves
team
y
x
population
id
description

我知道一个字典是无序的,但每次出现相同,我不知道为什么。

I know that a dictionary is unordered but it comes out the same every time and I've no idea why.

推荐答案

真正的问题应该是为什么不?无序字典最可能实现为哈希表 (实际上,Python 文档完全说明了这一点)元素定义明确,但并不明显。您的观察结果完美匹配哈希表的规则:明显的任意,但恒定的顺序。

The real question should be "why not?" … an unordered dictionary is most probably implemented as a hash table (in fact, the Python documentation states this outright) where the order of elements is well-defined but not immediately obvious. Your observations match the rules of a hash table perfectly: apparent arbitrary, but constant order.

这篇关于为什么python这样排序我的字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆