将字典键更新为升序?

Update dictionary keys to be in ascending order?

我有字典:

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'}}}

我想让二级key自动提升 像这样:

{'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'}}}

正如 martineau 所提到的,如果您想要保持数据集排序,您需要使用 collection/data 类型来保持顺序。对于这样的记录,您可以使用元组或命名元组。这些将允许您保持数据排序,并将它们放在列表中允许它们与允许您将未来数据插入排序集中的内置函数兼容。

普通词典是无序的,因此您需要使用 OrderedDict

全局 counter 变量可以跟踪条目总数。

sorted 函数将获取 key/value 个元组的列表并根据键对它们进行排序。

>>> 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'})]))
])

如果需要,您可以对最里面的变量进行排序,并将它们也存储在 OrderedDict 中。

如果你不需要保持顺序,你可以将字典复制到其他字典。一个全局变量可以保存二级密钥。

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

输出:

{
'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'}
}
}