本文介绍了如何对数字字符串的python列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试对包含数字的字符串列表进行排序
a = ["1099.0","9049.0"]a.sort()一种['1099.0', '9049.0']b = ["949.0","1099.0"]b.sort()乙['1099.0', '949.0']一种['1099.0', '9049.0']
但是列表 b
是排序而不是列表 a
解决方案
您想根据 float
值(不是字符串值)进行排序,请尝试:
I am trying to sort list of strings containing numbers
a = ["1099.0","9049.0"]
a.sort()
a
['1099.0', '9049.0']
b = ["949.0","1099.0"]
b.sort()
b
['1099.0', '949.0']
a
['1099.0', '9049.0']
But list b
is sorting and not list a
解决方案
You want to sort based on the float
values (not string values), so try:
>>> b = ["949.0","1099.0"]
>>> b.sort(key=float)
>>> b
['949.0', '1099.0']
这篇关于如何对数字字符串的python列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!