>>> My_dic = {752: (5,34),96:[1,2,{'wow':[6,9,178,-24,56], "Why":4,"much":[83,0,-56,7,98]}], 495:"too"}
>>> My_dic= res = My_dic(sorted(My_dic.items(),
key = lambda x: My_dic(x[1],[1])))
>>> My_dic= res = My_dic(sorted(My_dic.items(),key = lambda x: My_dic(x[1],["much"])
My_dic = {752: (5,34),96:{'wow':[6,9,178,-24,56], "Why":4,"much":[83,0,-56,7,98]}, 495:"too"}
我正在对python嵌套字典的部分顺序进行排序。我正在尝试使用sort在[much]旁边对[83,0,-56,7,98]进行排序。如果您可以帮助我对嵌套的字典部分进行排序,请多多关照。
答案 0 :(得分:0)
尝试这个伴侣
my_dict = {
752: (5,34),
96: {
'wow': [6, 9, 178, -24, 56],
"Why": 4,
"much": [83, 0, -56, 7, 98]
},
495: "too",
}
dict_keys = my_dict.keys()
for key in dict_keys:
if type(my_dict.get(key)) is dict and my_dict.get(key).get('much'):
my_dict[key]['much'] = sorted(my_dict[key]['much'])
print(my_dict)