PHP natsort 函数的 Python 模拟(使用“自然顺序”算法对列表进行排序)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2545532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:53:44  来源:igfitidea点击:

Python analog of PHP's natsort function (sort a list using a "natural order" algorithm)

pythonsortingnatsort

提问by Silver Light

I would like to know if there is something similar to PHP natsortfunction in Python?

我想知道Python中是否有类似于PHP natsort函数的东西?

l = ['image1.jpg', 'image15.jpg', 'image12.jpg', 'image3.jpg']
l.sort()

gives:

给出:

['image1.jpg', 'image12.jpg', 'image15.jpg', 'image3.jpg']

but I would like to get:

但我想得到:

['image1.jpg', 'image3.jpg', 'image12.jpg', 'image15.jpg']

UPDATE

更新

Solution base on this link

基于此链接的解决方案

def try_int(s):
    "Convert to integer if possible."
    try: return int(s)
    except: return s

def natsort_key(s):
    "Used internally to get a tuple by which s is sorted."
    import re
    return map(try_int, re.findall(r'(\d+|\D+)', s))

def natcmp(a, b):
    "Natural string comparison, case sensitive."
    return cmp(natsort_key(a), natsort_key(b))

def natcasecmp(a, b):
    "Natural string comparison, ignores case."
    return natcmp(a.lower(), b.lower())

l.sort(natcasecmp);

回答by jfs

From my answerto Natural Sorting algorithm:

自然排序算法的回答

import re
def natural_key(string_):
    """See http://www.codinghorror.com/blog/archives/001018.html"""
    return [int(s) if s.isdigit() else s for s in re.split(r'(\d+)', string_)]

Example:

例子:

>>> L = ['image1.jpg', 'image15.jpg', 'image12.jpg', 'image3.jpg']
>>> sorted(L)
['image1.jpg', 'image12.jpg', 'image15.jpg', 'image3.jpg']
>>> sorted(L, key=natural_key)
['image1.jpg', 'image3.jpg', 'image12.jpg', 'image15.jpg']

To support Unicode strings, .isdecimal()should be used instead of .isdigit(). See example in @phihag's comment. Related: How to reveal Unicodes numeric value property.

要支持 Unicode 字符串,.isdecimal()应使用.isdigit(). 请参阅@phihag 的评论中的示例。相关:如何显示 Unicodes 数值属性

.isdigit()may also fail (return value that is not accepted by int()) for a bytestring on Python 2 in some locales e.g., '\xb2' ('2') in cp1252 locale on Windows.

.isdigit()int()在某些语言环境中,Python 2 上的字节也可能失败(返回值不被 接受),例如Windows 上 cp1252 语言环境中的 '\xb2' ('2')

回答by SethMMorton

You can check out the third-party natsortlibrary on PyPI:

你可以在 PyPI 上查看第三方natsort库:

>>> import natsort
>>> l = ['image1.jpg', 'image15.jpg', 'image12.jpg', 'image3.jpg']
>>> natsort.natsorted(l)
['image1.jpg', 'image3.jpg', 'image12.jpg', 'image15.jpg']

Full disclosure, I am the author.

完全公开,我是作者。

回答by phihag

This function can be used as the key=argument for sortedin Python 2.x and 3.x:

此函数可用作Python 2.x 和 3.x 中的key=参数sorted

def sortkey_natural(s):
    return tuple(int(part) if re.match(r'[0-9]+$', part) else part
                for part in re.split(r'([0-9]+)', s))