为什么python这么订购我的字典?(复制)

时间:2021-12-24 00:20:41

This question already has an answer here:

这个问题已经有了答案:

Here is the dictionary I have

这是我的字典

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",
}

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

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

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.

我知道字典是无序的,但每次都是一样的,我不知道为什么。

3 个解决方案

#1


79  

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文档明确地说明了这一点),其中元素的顺序是明确定义的,但不是立即明显的。您的观察完全符合哈希表的规则:显然是任意的,但是顺序是不变的。

#2


10  

The specification for the built-in dictionary type disclaims any preservation of order, it is best to think of a dictionary as an unordered set of key: value pairs...

内置字典类型的规范不要求保留任何顺序,最好将字典看作一组无序的键:值对……

You may want to check the OrderedDict module, which is an implementation of an ordered dictionary with Key Insertion Order.

您可能需要检查OrderedDict模块,它是具有键插入顺序的有序字典的实现。

#3


8  

The only thing about dictionary ordering you can rely on is that the order will remain the same if there are no modifications to the dictionary; e.g., iterating over a dictionary twice without modifying it will result in the same sequence of keys. However, though the order of Python dictionaries is deterministic, it can be influenced by factors such as the order of insertions and removals, so equal dictionaries can end up with different orderings:

关于字典排序,您唯一可以依赖的是,如果没有对字典进行修改,那么顺序将保持不变;例:遍历一个字典两次而不修改它会导致相同的键序列。但是,虽然Python字典的顺序是确定的,但是它也会受到插入和删除顺序等因素的影响,所以相等的字典最终会有不同的顺序:

>>> {1: 0, 2: 0}, {2: 0, 1: 0}
({1: 0, 2: 0}, {1: 0, 2: 0})
>>> {1: 0, 9: 0}, {9: 0, 1: 0}
({1: 0, 9: 0}, {9: 0, 1: 0})

#1


79  

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文档明确地说明了这一点),其中元素的顺序是明确定义的,但不是立即明显的。您的观察完全符合哈希表的规则:显然是任意的,但是顺序是不变的。

#2


10  

The specification for the built-in dictionary type disclaims any preservation of order, it is best to think of a dictionary as an unordered set of key: value pairs...

内置字典类型的规范不要求保留任何顺序,最好将字典看作一组无序的键:值对……

You may want to check the OrderedDict module, which is an implementation of an ordered dictionary with Key Insertion Order.

您可能需要检查OrderedDict模块,它是具有键插入顺序的有序字典的实现。

#3


8  

The only thing about dictionary ordering you can rely on is that the order will remain the same if there are no modifications to the dictionary; e.g., iterating over a dictionary twice without modifying it will result in the same sequence of keys. However, though the order of Python dictionaries is deterministic, it can be influenced by factors such as the order of insertions and removals, so equal dictionaries can end up with different orderings:

关于字典排序,您唯一可以依赖的是,如果没有对字典进行修改,那么顺序将保持不变;例:遍历一个字典两次而不修改它会导致相同的键序列。但是,虽然Python字典的顺序是确定的,但是它也会受到插入和删除顺序等因素的影响,所以相等的字典最终会有不同的顺序:

>>> {1: 0, 2: 0}, {2: 0, 1: 0}
({1: 0, 2: 0}, {1: 0, 2: 0})
>>> {1: 0, 9: 0}, {9: 0, 1: 0}
({1: 0, 9: 0}, {9: 0, 1: 0})