【发布时间】:2018-09-19 10:21:54
【问题描述】:
在 python 中,如何快速将嵌套字典排序为 OrderedDict?例如:
{'susan': {'number': '5', 'fav_color': 'yellow'},
'josh': {'number': '1', 'fav_color': 'blue'},
'casey': {'number': '11', 'fav_color': 'orange'}}
我想对数字进行排序,最终得到:
{'josh': {'number': '1', 'fav_color': 'blue'},
'susan': {'number': '5', 'fav_color': 'yellow'},
'casey': {'number': '11', 'fav_color': 'orange'}}
【问题讨论】:
-
所以....你有什么尝试吗?
-
为什么要快速排序而不是使用普通的 timsort?
-
您能否调整this question 的答案以满足您的需求?
-
但无论哪种方式,答案都是相似的:不是对
sorted(d.items(), key=whatever)进行字典理解,而是对some_quicksort_function_you_wrote_or_found(d.items(), key=whatever)进行字典理解。 -
这是对特定键进行排序的又一个案例——在这种情况下,它是
int(elem['number'])。网上有无数的教程和例子。你到底卡在哪里了?
标签: python dictionary ordereddictionary