为什么字典值不符合插入顺序?

发布于 2024-11-08 04:25:44 字数 405 浏览 4 评论 0原文

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

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

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}

'a' 究竟是如何成为第一个元素而 'c' 的,即使它按字母顺序排序字典它应该是 1,2,3,4 或 a,b,c,d 而不是 1,3,2, 4. wT?F @!$!@$#@!

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

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.

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 @!$!@$#@!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

成熟稳重的好男人 2024-11-15 04:25:45

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

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).

油焖大侠 2024-11-15 04:25:45

字典排序顺序未定义!不要依赖它来做任何事情。如果您确实想要一本排序字典,请寻找一本排序字典,但通常您不需要一本。

示例:

  • python 2.7,它内置于集合模块中
  • Django 附带了一个 SortedDict
  • 2.4-2.7 您可以使用ordereddict 模块,您可以 pip install 或 easy_install 它

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
  • Django has a SortedDict shipped with it
  • 2.4-2.7 you can use the ordereddict module, you can pip install or easy_install it
尴尬癌患者 2024-11-15 04:25:45

在你如此生气和沮丧之前,也许你应该阅读一下字典到底是什么以及它是如何工作的:

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

Python dict 使用哈希表作为底层存储机制。这意味着哈希密钥是根据您提供的密钥生成的。无法保证使用这些哈希键进行排序。当您请求 value()、keys() 或 items() 时,字典中的条目将按其在基础哈希表中的位置顺序获取。

使用哈希表的优点是速度非常快。与 C++ 中使用红黑树存储机制(按原始键排序)的映射类不同,哈希表不需要经常重组以保持高效。有关哈希表的更多信息,请参阅:

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

就像其他海报所说的那样,如果您需要一个按键排序的字典,请查找 OrderedDict。

祝你好运!

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

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().

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:

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.

Good Luck!

春风十里 2024-11-15 04:25:45

显然您了解列表。您可以请求列表中第 i 个索引处的元素。这是因为列表是有序的。

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

在这种情况下,您可以想到字典,但其中索引是关键。因此,如果两个字典中所有键的对应值相同,则两个字典相等(如果一个字典具有另一个字典没有的键,则两者不相等)。因此:

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

进一步的琐事

字典对字典,以便当您请求特定键(索引)处的值时,它可以更快地检索该值。

希望这有帮助

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

>>> [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

甜心 2024-11-15 04:25:45

字典未排序。这是有据可查的。不要依赖字典的顺序。

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

哆啦不做梦 2024-11-15 04:25:45

如果您想按顺序查看条目。像这样的东西:(

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

剪切、粘贴、调味)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文