如何正确地对包含数字的字符串进行排序?

170

我有一个包含数字的字符串列表,但我找不到一个好的方法来对它们进行排序。
例如,我得到了类似于这样的东西:

something1
something12
something17
something2
something25
something29

使用sort()方法。

我知道可能需要先提取数字,然后再对列表进行排序,但我不知道如何用最简单的方式做到这一点。


sort()有什么问题? - tMC
7
这有一个名字,叫做自然排序。请参见 https://dev59.com/-3E85IYBdhLWcg3w64EA 和 https://dev59.com/aG445IYBdhLWcg3we6V9 ,可能还有其他类似的网页。 - Mark Ransom
6
为什么不直接使用list_name.sort(key=lambda x: float(x.strip('something'))) - altroware
1个回答

351

也许你正在寻找人类排序(也称为自然排序):

import re

def atoi(text):
    return int(text) if text.isdigit() else text

def natural_keys(text):
    '''
    alist.sort(key=natural_keys) sorts in human order
    http://nedbatchelder.com/blog/200712/human_sorting.html
    (See Toothy's implementation in the comments)
    '''
    return [ atoi(c) for c in re.split(r'(\d+)', text) ]

alist=[
    "something1",
    "something12",
    "something17",
    "something2",
    "something25",
    "something29"]

alist.sort(key=natural_keys)
print(alist)

产生
['something1', 'something2', 'something12', 'something17', 'something25', 'something29']

PS. 我已经改变了我的答案,使用Toothy在评论区发布的自然排序实现这里,因为它比我的原始答案快得多。


如果您希望使用浮点数对文本进行排序,则需要将正则表达式从匹配整数的表达式(即(\d+))更改为可以匹配浮点数的正则表达式

import re

def atof(text):
    try:
        retval = float(text)
    except ValueError:
        retval = text
    return retval

def natural_keys(text):
    '''
    alist.sort(key=natural_keys) sorts in human order
    http://nedbatchelder.com/blog/200712/human_sorting.html
    (See Toothy's implementation in the comments)
    float regex comes from https://dev59.com/FGcs5IYBdhLWcg3wtmID#12643073
    '''
    return [ atof(c) for c in re.split(r'[+-]?([0-9]+(?:[.][0-9]*)?|[.][0-9]+)', text) ]

alist=[
    "something1",
    "something2",
    "something1.0",
    "something1.25",
    "something1.105"]

alist.sort(key=natural_keys)
print(alist)

产出。
['something1', 'something1.0', 'something1.105', 'something1.25', 'something2']

我也可以使用上述方法对具有子属性(字符串)的对象列表进行排序。只需将“text”替换为“someobject”,然后return [ atoi(c) for c in re.split('(\d+)', someobject.sometextproperty) ]即可。 - Jonny
2
你知道如何将这个扩展到数字是浮点数的情况吗?例如,something1.0,something 1.25,something2.0。 - painfulenglish
4
我已经修改了上面的帖子,展示如何使用浮点数进行自然排序文本。 - unutbu
我已经使用了上述代码来对我的列表进行排序,但是有什么想法为什么我不能首先删除重复的条目,即:attr1 = set(all_names) attr1.sort(key=natural_keys)。 - 2one
attr1 = set(...) 会将 attr1 变成一个集合。集合没有 sort 方法,所以 attr1.sort 会引发 AttributeError。可以尝试使用 attr1 = list(set(all_names)),因为列表确实有 sort 方法。 - unutbu
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接