【发布时间】:2025-12-29 08:45:07
【问题描述】:
sorted_event_types={}
for key,value in sorted(event_types.items()):
sorted_event_types[key]=value
(Pdb) print sorted_event_types
{'ch': 1, 'eh': 2, 'oh': 3, 'ah': 0, 'as': 1, 'cs': 0, 'os': 5, 'es': 9}
(Pdb) event_types
{'as': 1, 'ch': 1, 'eh': 2, 'oh': 3, 'cs': 0, 'ah': 0, 'os': 5, 'es': 9}
我想对字典进行排序并保持这种方式。
输出应该是{'ah': 1, 'as': 1, 'ch': 2, 'cs': 3, 'eh': 0, ...}
【问题讨论】:
-
那么问题到底是什么?
-
见OrderedDict(虽然我可能会把它变成一个列表,因为我喜欢分离)
-
遗憾的是,重复的问题有 OrderedDict(无论是否合适)的答案,而被埋没了..
-
恕我直言,重复的问题与 OP 的要求无关。我认为this one 更相关。
标签: python string python-2.7 dictionary