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
Python analog of PHP's natsort function (sort a list using a "natural order" algorithm)
提问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')。