为什么字典值不按插入顺序排列?

时间:2022-11-04 13:03:12

When i declare a list 1,2,3,4 and i do something with it , even just print i get back the same sequence 1,2,3,4.

当我声明一个列表1 2 3 4时,我对它做了一些处理,即使只是打印我也会得到相同的序列1 2 3 4。

But when i do anything with dictionaries , they always change number sequence , like it is being sorted in a twisted way i can't understand .

但是当我用字典做任何事情时,它们总是改变数字序列,就像它被以一种我无法理解的扭曲方式排序一样。

test1 = [4,1,2,3,6,5]
print test1
test2 = {"c":3,"a":1,"b":2,"d":4}
print test2 

[4, 1, 2, 3, 6, 5]
{'a': 1, 'c': 3, 'b': 2, 'd': 4}

How in the world did 'a' become the first element and 'c' , even if it alphabetically sorted the dictionary it should have been 1,2,3,4 or a,b,c,d not 1,3,2,4 . wT?F @!$!@$#@!

“a”怎么变成了第一个元素和“c”呢?即使按字母顺序排列字典也应该是1、2、3、4或者a、b、c、d而不是1、3、2、4。wT吗?F @ ! $ @ $ # @ !

So how do i print , get values from dictionary without changing the positions of the elements .?

那么如何打印,从字典中获取值而不改变元素的位置呢?

6 个解决方案

#1


12  

Dictionaries in Python are unordered by definition. Use OrderedDict if you need the order in which values were inserted (it's available in Python 2.7 and 3.x).

Python中的字典根据定义是无序的。如果需要插入值的顺序,请使用OrderedDict(在Python 2.7和3.x中可用)。

#2


3  

dictionary sort order is undefined! Do not rely on it for anything. Look for a sorted dictionary if you really want a sorted dictionary, but usually you don't need one.

字典排序顺序未定义!不要依赖它做任何事。如果你真的想要一个已排序的字典,你可以找一个已排序的字典,但是通常你不需要一个。

Examples:

例子:

  • python 2.7, it's built in to the collections module
  • python 2.7,它内置在集合模块中
  • Django has a SortedDict shipped with it
  • Django有一个附带的目录
  • 2.4-2.7 you can use the ordereddict module, you can pip install or easy_install it
  • 2.4-2.7可以使用ordereddict模块,可以pip安装或easy_install

#3


1  

Before you get so angry and frustrated, perhaps you should read about what a dictionary actually is and how it works:

在你变得如此愤怒和沮丧之前,也许你应该读一下字典实际上是什么以及它是如何工作的:

http://docs.python.org/library/stdtypes.html#mapping-types-dict

http://docs.python.org/library/stdtypes.html mapping-types-dict

Python dicts use a hash table as the underlying storage mechanism. That means that a hash key is generated from the key that you provide. There are no guarantees about ordering with these hash keys. The entries in a dictionary are fetched in sequential order of their location in the underlying hash table when you request values(), keys(), or items().

Python命令使用散列表作为底层存储机制。这意味着一个散列键是由您提供的密钥生成的。使用这些哈希键排序没有保证。当您请求值()、keys()或items()时,字典中的条目按顺序在底层哈希表中获取。

The advantage of using a hash table is that it is extremely fast. Unlike the map class from c++ which uses a red-black tree storage mechanism ( which is sorted by the raw keys ), a hash table doesn't constantly need to be restructured to keep it efficient. For more on hash tables, see:

使用哈希表的优点是它非常快。与使用红黑树存储机制(按原始键排序)的c++中的map类不同,哈希表不需要不断地重新构造以保持效率。有关哈希表的更多信息,请参见:

http://en.wikipedia.org/wiki/Hash_table

http://en.wikipedia.org/wiki/Hash_table

Like the other posters have said, look up OrderedDict if you need to have a key-sorted dictionary.

就像其他的海报说的那样,如果你需要有一个键分类的字典,你可以查一下OrderedDict。

Good Luck!

好运!

#4


1  

Clearly you know about lists. You can ask for the element at the ith index of a list. This is because lists are ordered.

显然你知道列表。您可以在列表的第i个索引处请求元素。这是因为列表是有序的。

>>> [1,2,3,4] == [1,4,3,2]
False

In this context, you can think of dictionaries, but where the index is the key. Therefore, two dictionaries are equal if the corresponding values of all keys in both dictionaries are the same (if one dictionary has keys that the other doesn't, then the two are not equal). Thus:

在这种情况下,您可以考虑字典,但是索引是关键。因此,如果两个字典中所有键的对应值都相同(如果一个字典有另一个字典没有的键,那么两个字典就不相等),那么两个字典就相等。因此:

>>> {1:'a', 2:'b'} == {2:'b', 1:'a'}
True

Further Trivia

进一步的琐事

A dictionary does something called hashing on the keys of the dictionary so that when you ask for the value at a particular key (index), it can retrieve this value faster.

字典对字典的键进行哈希操作,以便当您在一个特定的键(索引)处请求值时,它可以更快地检索这个值。

Hope this helps

希望这有助于

#5


0  

Dictionaries are unsorted. This is well-documented. Do not rely on the ordering of dictionaries.

字典是无序的。这是证据确凿的。不要依赖字典的顺序。

#6


0  

If you want to see the entries in order. something like:

如果您想按顺序查看条目。喜欢的东西:

test2 = {"c":3,"a":1,"b":2,"d":4}
ks = test2.keys()
ks.sort()
for key in ks:
   print key + ':' + str(test2[key])

(cut,paste, season to taste)

(剪切、粘贴,按你的口味)

#1


12  

Dictionaries in Python are unordered by definition. Use OrderedDict if you need the order in which values were inserted (it's available in Python 2.7 and 3.x).

Python中的字典根据定义是无序的。如果需要插入值的顺序,请使用OrderedDict(在Python 2.7和3.x中可用)。

#2


3  

dictionary sort order is undefined! Do not rely on it for anything. Look for a sorted dictionary if you really want a sorted dictionary, but usually you don't need one.

字典排序顺序未定义!不要依赖它做任何事。如果你真的想要一个已排序的字典,你可以找一个已排序的字典,但是通常你不需要一个。

Examples:

例子:

  • python 2.7, it's built in to the collections module
  • python 2.7,它内置在集合模块中
  • Django has a SortedDict shipped with it
  • Django有一个附带的目录
  • 2.4-2.7 you can use the ordereddict module, you can pip install or easy_install it
  • 2.4-2.7可以使用ordereddict模块,可以pip安装或easy_install

#3


1  

Before you get so angry and frustrated, perhaps you should read about what a dictionary actually is and how it works:

在你变得如此愤怒和沮丧之前,也许你应该读一下字典实际上是什么以及它是如何工作的:

http://docs.python.org/library/stdtypes.html#mapping-types-dict

http://docs.python.org/library/stdtypes.html mapping-types-dict

Python dicts use a hash table as the underlying storage mechanism. That means that a hash key is generated from the key that you provide. There are no guarantees about ordering with these hash keys. The entries in a dictionary are fetched in sequential order of their location in the underlying hash table when you request values(), keys(), or items().

Python命令使用散列表作为底层存储机制。这意味着一个散列键是由您提供的密钥生成的。使用这些哈希键排序没有保证。当您请求值()、keys()或items()时,字典中的条目按顺序在底层哈希表中获取。

The advantage of using a hash table is that it is extremely fast. Unlike the map class from c++ which uses a red-black tree storage mechanism ( which is sorted by the raw keys ), a hash table doesn't constantly need to be restructured to keep it efficient. For more on hash tables, see:

使用哈希表的优点是它非常快。与使用红黑树存储机制(按原始键排序)的c++中的map类不同,哈希表不需要不断地重新构造以保持效率。有关哈希表的更多信息,请参见:

http://en.wikipedia.org/wiki/Hash_table

http://en.wikipedia.org/wiki/Hash_table

Like the other posters have said, look up OrderedDict if you need to have a key-sorted dictionary.

就像其他的海报说的那样,如果你需要有一个键分类的字典,你可以查一下OrderedDict。

Good Luck!

好运!

#4


1  

Clearly you know about lists. You can ask for the element at the ith index of a list. This is because lists are ordered.

显然你知道列表。您可以在列表的第i个索引处请求元素。这是因为列表是有序的。

>>> [1,2,3,4] == [1,4,3,2]
False

In this context, you can think of dictionaries, but where the index is the key. Therefore, two dictionaries are equal if the corresponding values of all keys in both dictionaries are the same (if one dictionary has keys that the other doesn't, then the two are not equal). Thus:

在这种情况下,您可以考虑字典,但是索引是关键。因此,如果两个字典中所有键的对应值都相同(如果一个字典有另一个字典没有的键,那么两个字典就不相等),那么两个字典就相等。因此:

>>> {1:'a', 2:'b'} == {2:'b', 1:'a'}
True

Further Trivia

进一步的琐事

A dictionary does something called hashing on the keys of the dictionary so that when you ask for the value at a particular key (index), it can retrieve this value faster.

字典对字典的键进行哈希操作,以便当您在一个特定的键(索引)处请求值时,它可以更快地检索这个值。

Hope this helps

希望这有助于

#5


0  

Dictionaries are unsorted. This is well-documented. Do not rely on the ordering of dictionaries.

字典是无序的。这是证据确凿的。不要依赖字典的顺序。

#6


0  

If you want to see the entries in order. something like:

如果您想按顺序查看条目。喜欢的东西:

test2 = {"c":3,"a":1,"b":2,"d":4}
ks = test2.keys()
ks.sort()
for key in ks:
   print key + ':' + str(test2[key])

(cut,paste, season to taste)

(剪切、粘贴,按你的口味)