对key中有数字的字典进行排序

Posted 明天OoO你好

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对key中有数字的字典进行排序相关的知识,希望对你有一定的参考价值。

word_cloud = []
cc = [{"c58":341,"c59":525,"c56":507,"c57":341,"c54":639,"c55":499,"c23":542,"c63":751,"c64":815,"c17":306,"c16":360,"c19":593,"c18":330,"c11":427,"c10":443,"c13":396,"c12":451,"c15":352,"c14":416,"c32":776,"c33":770,"c30":354,"c31":760,"c36":542,"c37":363,"c34":806,"c35":506,"c38":379,"c39":489,"c40":559,"c41":419,"c42":441,"c43":419,"c44":447,"c45":499,"c46":527,"c47":580,"c48":663,"c49":489,"c6":353,"c5":261,"c4":308,"c3":280,"c9":377,"c8":242,"c7":238,,"c51":542,"c50":625}]

sortdata = sorted(cc[0].items(),key=lambda item:int(item[0][1:]))
for item in sortdata:
word_cloud.append({item[0]:item[1]})
print word_cloud


输出:
[{‘c3‘: 280}, {‘c4‘: 308}, {‘c5‘: 261}, {‘c6‘: 353}, {‘c7‘: 238}, {‘c8‘: 242}, {‘c9‘: 377}, {‘c10‘: 443}, {‘c11‘: 427}]

以上是关于对key中有数字的字典进行排序的主要内容,如果未能解决你的问题,请参考以下文章

在 C# Windows 窗体中对字典进行排序[重复]

按自然顺序对字典键进行排序[重复]

对数组中的字典进行排序

mapreduce 实现数子排序

Python对字典分别按键(key)和值(value)进行排序

如何一次对字典的所有键、子键、子子键等进行排序?