gpt4 book ai didi

python - 我如何首先根据它们的单位(bit/s、Kbit/s 等)对特定的字典列表进行排序,然后根据它们的值对它们进行排序

转载 作者:行者123 更新时间:2023-12-05 09:31:17 25 4
gpt4 key购买 nike

my_lst = [
{"usage": "0.59 Kbit/s"},
{"usage": "383.34 bit/s"},
{"usage": "158.13 bit/s"},
{"usage": "4.15 Gbit/s"},
{"usage": "18.5 Mbit/s"},
{"usage": "45 Kbit/s"}
]

尝试实现的输出应该是这样的,带有排序值:-

my_lst = [
{'usage': '4.15 Gbit/s'},
{'usage': '18.5 Mbit/s'},
{'usage': '45 Kbit/s'},
{'usage': '0.59 Kbit/s'},
{'usage': '383.34 bit/s'},
{'usage': '158.13 bit/s'}
]

最佳答案

单向使用 humanfriendly.parse_size :

from humanfriendly import parse_size

sorted(my_lst, key=lambda x: parse_size(x["usage"]), reverse=True)

输出:

[{'usage': '4.15 Gbit/s'},
{'usage': '18.5 Mbit/s'},
{'usage': '45 Kbit/s'},
{'usage': '0.59 Kbit/s'},
{'usage': '383.34 bit/s'},
{'usage': '158.13 bit/s'}]

关于python - 我如何首先根据它们的单位(bit/s、Kbit/s 等)对特定的字典列表进行排序,然后根据它们的值对它们进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68933393/

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