因此,基本上keys
需要sorted
在nested dict
,通過字典並將數據傳輸到一個新的字典。
#driver值
IN : d
{
'test' :{ 0:[1,3] ,
1:[5,6]
},
'test2':{ 7:[9],
3:[4,6]
}
}
OUT : new_d
OrderedDict([('test', {0: [1, 3], 1: [5, 6]}), ('test2', {3: [4, 6], 7: [9]})])
編輯:由於OP想要字典的初始密鑰(EX:test3:{ ... } , test2:{ ... }
)進行過整理,需要下面的變化來完成:
>>> initial_sort = OrderedDict(sorted(d.items()))
>>> inital_sort
OrderedDict([('test2', {7: [9], 3: [4, 6]}), ('test3', {0: [1, 3], 1: [5, 6]})])
>>> new_d = OrderedDict()
>>> for key,val in initial_sort.items(): #go through the first key sorted dictionary
new_d[key] = OrderedDict(sorted(val.items()))
#驅動值
IN : d = {'test3': {0: [1, 3], 1: [5, 6]}, 'test2': {7: [9], 3: [4, 6]}}
OUT : new_d = OrderedDict([('test2', OrderedDict([(3, [4, 6]), (7, [9])])), ('test3', OrderedDict([(0, [1, 3]), (1, [5, 6])]))])
等等,'test2'與'test'在這裏有什麼關係? –
@ KaushikNP基本上有一個字典,其中有「test」和「test2」鍵。這些鍵中的每一個都作爲內部字典的值保存。因此,例如dict [「test」] = {0:[1,3],1:[5,6]} –
基本上,按字典中的鍵進行排序吧? –