Python intensive training notes (6)-keep the dictionary in order and enhance python Training
The python dictionary is a very convenient data structure. With it, we can easily find his score, ranking, and other values based on the name (key ), instead of traversing the entire dataset.
Example: {'lil': [1,100], 'jar': [2, 98]...}
However, there are some problems in the process of using the dictionary, that is, the dictionary itself does not matter the order you enter
>>> D ={}>> d ['Lee '] = [1,100] >>> d ['Jane'] = [2, 98] >>> d ['Tom '] = [1,100] >>> d {'jar': [2, 98], 'lil': [], 'Tom ': [3, 96]}
You can use OrderedDict of the collections module to meet this requirement.
>>> From collections import OrderedDict >>> d = OrderedDict () >>> d ['lil'] = [1,100] >>> d ['jar'] = [2, 98] >>> d ['Tom '] = [1,100] >>> dOrderedDict ([('Lee', []), ('Jane ', [2, 98]), ('Tom ', [3, 96])