PHP natsort 函数的 Python 模拟(使用“自然顺序"算法对列表进行排序) [英] Python analog of PHP's natsort function (sort a list using a "natural order" algorithm)

查看:31
本文介绍了PHP natsort 函数的 Python 模拟(使用“自然顺序"算法对列表进行排序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

给出:

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

但我想得到:

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

更新

基于此链接

的解决方案

def try_int(s):如果可能,转换为整数."尝试:返回整数除了:返回 sdef natsort_key(s):在内部用于获取 s 排序所依据的元组."进口重新return map(try_int, re.findall(r'(d+|D+)', s))def natcmp(a, b):自然字符串比较,区分大小写."返回 cmp(natsort_key(a), natsort_key(b))def natcasecmp(a, b):自然字符串比较,忽略大小写."返回 natcmp(a.lower(), b.lower())l.sort(natcasecmp);

解决方案

来自我的回答自然排序算法:

导入重新def natural_key(string_):"""见 https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/"""return [int(s) if s.isdigit() else s for s in re.split(r'(d+)', string_)]

示例:

<预><代码>>>>L = ['image1.jpg', 'image15.jpg', 'image12.jpg', 'image3.jpg']>>>排序(L)['image1.jpg', 'image12.jpg', 'image15.jpg', 'image3.jpg']>>>排序(L,键=natural_key)['image1.jpg', 'image3.jpg', 'image12.jpg', 'image15.jpg']

为了支持 Unicode 字符串,应该使用 .isdecimal() 而不是 .isdigit().请参阅 @phihag 的评论.相关:如何显示 Unicodes 数值属性.

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

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

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);

解决方案

From my answer to Natural Sorting algorithm:

import re
def natural_key(string_):
    """See https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/"""
    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.

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

这篇关于PHP natsort 函数的 Python 模拟(使用“自然顺序"算法对列表进行排序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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