use*_*011 0 python sorting dictionary list
我有以下字典:
#original
random_dict={'list1': ['b', 'a', 'c'], 'list2': ['f', 'a', 'e'], 'list3': ['c', 'b', 'a']}
Run Code Online (Sandbox Code Playgroud)
你如何按字母顺序对列表进行排序random_dict以获得:
#after sorting
sorted_dict={'list1': ['a', 'b', 'c'], 'list2': ['a', 'e', 'f'], 'list3': ['a', 'b', 'c']}
Run Code Online (Sandbox Code Playgroud)
Mar*_*ers 11
只需调用.sort()每个值:
for val in random_dict.values():
val.sort()
Run Code Online (Sandbox Code Playgroud)
这就地改变random_dict了.如果您需要副本,请使用词典理解:
sorted_dict = {k: sorted(v) for k, v in random_dict.iteritems()}
Run Code Online (Sandbox Code Playgroud)
在python 3上,替换.iteritems()为.items().