简体   繁体   中英

Update dictionary keys to be in ascending order?

I have a dictionary:

A = {'cat': {0: {'variable_1': 'xxx', 'variable_2': 'yyy'},
             1: {'variable_1': 'ttt', 'variable_2': 'kkk'}},
     'dog': {0: {'variable_1': 'xxx', 'variable_2': 'ppp'},
             1: {'variable_1': 'qqq', 'variable_2': 'www'}},
     'fox': {0: {'variable_1': 'xxx', 'variable_2': 'zzz'},
             1: {'variable_1': 'yyy', 'variable_2': 'uuu'},
             3: {'variable_1': 'ccc', 'variable_2': 'jjj'}}}

I want to make the second level key automated ascend like this:

{'cat': {0: {'variable_1': 'xxx', 'variable_2': 'yyy'},
         1: {'variable_1': 'ttt', 'variable_2': 'kkk'}},
 'dog': {2: {'variable_1': 'xxx', 'variable_2': 'ppp'},
         3: {'variable_1': 'qqq', 'variable_2': 'www'}},
 'fox': {4: {'variable_1': 'xxx', 'variable_2': 'zzz'},
         5: {'variable_1': 'yyy', 'variable_2': 'uuu'},
         6: {'variable_1': 'ccc', 'variable_2': 'jjj'}}}

Regular dictionaries are unordered, so you will need to use OrderedDict .

A global counter variable can keep track of the total number of entries.

The sorted function will take a list of key/value tuples and sort them according to the key.

>>> from collections import OrderedDict
>>> A = {'cat': {0: {'variable_1': 'xxx', 'variable_2': 'yyy'},
                 1: {'variable_1': 'ttt', 'variable_2': 'kkk'}},
         'dog': {0: {'variable_1': 'xxx', 'variable_2': 'ppp'},
                 1: {'variable_1': 'qqq', 'variable_2': 'www'}},
         'fox': {0: {'variable_1': 'xxx', 'variable_2': 'zzz'},
                 1: {'variable_1': 'yyy', 'variable_2': 'uuu'},
                 3: {'variable_1': 'ccc', 'variable_2': 'jjj'}}}

>>> OA = OrderedDict()
>>> count = 0
>>> for animal, info in sorted(A.items()):
        OA[animal] = OrderedDict()
        for i, variables in sorted(info.items()):
            OA[animal][count] = variables
            count += 1

>>> OA
OrderedDict([
   ('cat', OrderedDict([(0, {'variable_2': 'yyy', 'variable_1': 'xxx'}), 
                        (1, {'variable_2': 'kkk', 'variable_1': 'ttt'})])),
   ('dog', OrderedDict([(2, {'variable_2': 'ppp', 'variable_1': 'xxx'}), 
                        (3, {'variable_2': 'www', 'variable_1': 'qqq'})])),
   ('fox', OrderedDict([(4, {'variable_2': 'zzz', 'variable_1': 'xxx'}),
                        (5, {'variable_2': 'uuu', 'variable_1': 'yyy'}),
                        (6, {'variable_2': 'jjj', 'variable_1': 'ccc'})]))
])

If needed you can sort the innermost variables and stored them in an OrderedDict as well.

As martineau mentioned, If you want to keep a data set sorted, you'll want to use a collection/data type that preserves order. For a record like this, you can use tuples or namedtuples. These will allow you to keep data sorted, and having them in a list allows them compatibility with built in functions that allow to you insert future data into an sorted set.

if u don't need to keep the order,u can copy the dict to other dict. A global variable can keep the second level key.

# coding=utf-8

A={'cat':{0:{'variable_1':'xxx','variable_2':'yyy'},1:{'variable_1':'ttt','variable_2':'kkk'}},
    'dog':{0:{'variable_1':'xxx','variable_2':'ppp'},1:{'variable_1':'qqq','variable_2':'www'}},
  'fox':{0:{'variable_1':'xxx','variable_2':'zzz'},1:{'variable_1':'yyy','variable_2':'uuu'},3:{'variable_1':'ccc','variable_2':'jjj'}}}

B = dict()
index = 0
for key in A:
    animal = A[key]
    B[key] = dict()
    for i in animal:
        B[key][index] = animal[i]
        index += 1
print(B)

output:

{
'dog': {
 0: {'variable_1': 'xxx', 'variable_2': 'ppp'}, 
 1: {'variable_1': 'qqq', 'variable_2': 'www'}
}, 
'fox': {
 2: {'variable_1': 'xxx', 'variable_2': 'zzz'}, 
 3: {'variable_1': 'yyy', 'variable_2': 'uuu'}, 
 4: {'variable_1': 'ccc', 'variable_2': 'jjj'}
}, 
'cat': {
 5: {'variable_1': 'xxx', 'variable_2': 'yyy'}, 
 6: {'variable_1': 'ttt', 'variable_2': 'kkk'}
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM