开发者

Why dictionary values aren't in the inserted order?

开发者 https://www.devze.com 2023-03-07 14:11 出处:网络
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.

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 .开发者_StackOverflow社区?


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


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


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!


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


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


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号