简体   繁体   English

使用Python3.x中的键值对DefaultDict()进行排序

[英]Sorting DefaultDict() using key values in Python3.x

I have a dict which contains values like 我有一个包含值的字典

DefaultListOrderedDict([('29.970', [1, 0, 0, 0, 0]), ('100.000', [0, 1, 0, 0, 1]), ('200.000', [0, 0, 1, 0, 0]), ('60.000', [0, 0, 0, 1, 0]), ('0.750', [0, 0, 1, 0, 0]), ('25.000', [0, 0, 0, 0, 1]), ('48.000', [0, 0, 1, 0, 0])])

I would like to sort it with values 我想按值对它进行排序

I tried for k,v in sorted(Dict.items(),reverse = True): 我尝试for k,v in sorted(Dict.items(),reverse = True):

But the sorting occurs in this order : 但是排序按以下顺序进行:

   ['0.750', '100.000', '200.000', '25.000', '29.970', '48.000', '60.000']

I don't understand the reason behind it. 我不明白其背后的原因。 I would like to know the way how to sort it as : 我想知道如何将其排序为:

   ['0.750', '25.000', '29.970', '48.000', '60.000', '100.000', '200.000']

您应该将键转换为浮点型:

OrderedDict(sorted(your_dict.items(), key=lambda item: float(item[0])))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM