且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何正确对里面有数字的字符串进行排序?

更新时间:2023-02-11 08:04:22

也许您正在寻找 human排序(也称为自然排序):

Perhaps you are looking for human sorting (also known as natural sorting):

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

附注.我已经改变了我的答案,以使用 Toothy 的自然排序实现(发表在评论中 here) 因为它比我原来的答案快得多.

PS. I've changed my answer to use Toothy's implementation of natural sorting (posted in the comments here) since it is significantly faster than my original answer.

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

If you wish to sort text with floats, then you'll need to change the regex from one that matches ints (i.e. (d+)) to a regex that matches floats:

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://***.com/a/12643073/190597
    '''
    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']