【发布时间】:2013-05-01 00:34:39
【问题描述】:
我有一个字典(也是一个更大字典的键),看起来像
wd[wc][dist][True]={'course': {'#': 1, 'Fisher': 4.0},
'i': {'#': 1, 'Fisher': -0.2222222222222222},
'of': {'#': 1, 'Fisher': 2.0},
'will': {'#': 1, 'Fisher': 3.5}}
我想按相应的“Fisher”值对关键词(最高级别)进行排序... 这样输出看起来像
wd[wc][dist][True]={'course': {'Fisher': 4.0, '#': 1}, 'will': {'Fisher': 3.5, '#': 1}, 'of': {'Fisher': 2.0, '#': 1}, 'i': {'Fisher': -0.2222222222222222, '#': 1}}
我尝试过使用 items() 和 sorted() 但无法解决... 请帮帮我:(
【问题讨论】:
-
很遗憾,您无法对字典进行排序,它是无序的。阅读这篇很棒的帖子,了解如何操作:stackoverflow.com/questions/613183/…
-
2021:对于 Python >=3.7,该语言指定字典是保留顺序的,而 CPython 实现了这一点。
标签: python sorting dictionary