我正在尝试对包含数字的字符串列表进行排序

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

但是列表b是排序而不是列表a

最佳答案

您要基于float值(而不是字符串值)进行排序,因此请尝试:

>>> b = ["949.0","1099.0"]
>>> b.sort(key=float)
>>> b
['949.0', '1099.0']

关于python - 如何对数字字符串的python列表进行排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17474211/

10-10 11:54