gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:51:48 25 4
gpt4 key购买 nike

我有一个字典,其中包含类似的值

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])])

我想用值对其进行排序

我尝试在sorted(Dict.items(),reverse = True)中查找k,v:

但排序是按以下顺序进行的:

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

我不明白其背后的原因。我想知道如何将其排序为:

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

最佳答案

您应该将键转换为 float :

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

关于python - Python3.x 中使用键值对 DefaultDict() 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28991492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com