仅在使用其子键按字母顺序对嵌套字典进行排序时,确实很费劲。我在这里找不到类似的问题。
例如,我有:
people = {5: {'first': 'John', 'age': '27', 'last': 'Doe'},
2: {'first': 'Marie', 'age': '22', 'gender': 'Female'}}
但是想要:
people = {5: {'age': '27','first': 'John', 'last': 'Doe'},
2: {'age': '22','first': 'Marie', 'gender': 'Female'}}
尝试:
import OrderedDict from collections
for d in people:
people[d] = OrderedDict(sorted(d.items()))
AttributeError: 'int' object has no attribute 'items'
答案 0 :(得分:1)
我知道了:
import OrderedDict from collections
for data_center in dc_error_modes:
d = dc_error_modes[data_center]
dc_error_modes[data_center] = OrderedDict(sorted(d.items()))