如何正确排序带数字的字符串? [英] How to correctly sort a string with a number inside?

查看:634
本文介绍了如何正确排序带数字的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
Python是否具有用于自然字符串的内置函数排序?

Possible Duplicate:
Does Python have a built in function for string natural sort?

我有一个包含数字的字符串列表,我找不到一种对它们进行排序的好方法.
例如,我得到这样的东西:

I have a list of strings containing numbers and I cannot find a good way to sort them.
For example I get something like this:

something1
something12
something17
something2
something25
something29

使用sort()方法.

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

I know that I probably need to extract the numbers somehow and then sort the list but I have no idea how to do it in the most simple way.

推荐答案

也许您正在寻找 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']

PS.我已经更改了答案,以使用Toothy的自然排序实现(发布在评论此处 ),因为它比我原来的答案要快得多.

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://stackoverflow.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']

这篇关于如何正确排序带数字的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆