OrderedDict ordered dictionary and how to keep the original order when reading json string

OrderedDict ordered dictionary and how to keep the original order when reading json string
OrderedDict is a subclass of dict, which remembers the order in which the content was added.
When comparing, the OrderedDict must be identical in content and order to be considered equal.

Example:

import collections  
d = collections.OrderedDict()  
d[3] = 'A'  
d[2] = 'B'  
d[1] = 'C'  

for k, v in d.items():  
   print k, v  

Result:
3 A
2 B
1 C

whereas if d is a general dict, the result is:

1 C
2 B
3 A

2. How to keep the original order when reading the json string

import json  
from collections import OrderedDict  
metadata = json.loads(text, object_pairs_hook=OrderedDict);  

The order of properties in metadata is the same as the order defined in text.

For details, see the parameters in the json.loads function in the python documentation

Python 3.6 has rewritten the internal algorithm of dict, so the dict of 3.6 is ordered, and it was unordered before this version (the latest version of the answer is 3.6), please refer to read PEP 468 According to PEP 468, this improvement reduces the dict The memory consumption is about 20%-25% (compared to Python 3.5). Note that the order followed by the dict of 3.6 is the insertion order of the Key. That is to say, you need to insert in a certain order to get the sorted results, otherwise you still need to sort. In addition, the set, which is also implemented by the Hash table, is still out of order in 3.6. This has not been changed. Finally, the lower version hopes that the ordered dict can use collections.OrderedDict. This is a container that was implemented as early as 2.7. It also follows insertion order.

Author: Aise Eri
Link : https://www.zhihu.com/question/24306558/answer/374075597
Source: Zhihu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

The essence of the dictionary is that
the data structure of the hash table is destined to be unordered.
As for the internal structure and implementation of the hash table, you can refer to "Introduction to Algorithms".

Author: Anonymous
Link : https://www.zhihu.com/question/24306558/answer/27358613
Source: Zhihu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

What is written in that book... (Hash is referred to as Hash Table in the following) The idea of ​​Hash is very simple, that is, to apply a kernel with index sorting, apply for an Array, and use the position of the Array as a kind of metadata when sorting by index, it is very Simple, the size of the value is the position of the array. For example, I have int A[6], I want to sort 2 5 4 8, put these numbers into the 2 5 4 8 bits of the Array, and then read them out in order , the sequence is arranged at a speed of O(n). The difference between Hash and Index is that the mapping from the value to the Array position is different. In Index sorting, the position of the value in the Array is the value itself in the Hash, which is the change algorithm, use the Key to calculate the position in the Array, and put the Value. For example, my Key is 10. In Hash, the Value may be placed in the Array position 2. Under this algorithm, the insertion order is unestimable. First In the second operation, Key 2 Value 90 is inserted into Hash Table A, and the calculated position is 5. The second operation, Key 9 Value 10 is inserted into Hash Table A, and the calculated position is 3. Have you found it? Hash Table does not guarantee insertion at all. The data that comes later in the sequence can be run before the data inserted first. The essence of Hash is to use a sequence as a key to evenly allocate another sequence Value to the Array that the program has applied for. Finally, irrelevant, also Causes the dictionary to be out of order. Python will automatically expand the dictionary.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325990020&siteId=291194637