Python3粗略实现【windows 文件名称排序】的风格,自

2020-05-18  本文已影响0人  青耘丶
import re
import functools

def natural_key(astr):
    return [int(s) if s.isdecimal() else s for s in re.split(r'(\d+)', astr)]


def natural_cmp(a, b):
    key_a = natural_key(a)
    key_b = natural_key(b)
    if key_a > key_b:
        return 1
    if key_a < key_b:
        return -1
    if key_a == key_b:
        return 0


def tcmp(a, b):
    return natural_cmp(a["name"], b["name"])

results=[]
sorted(results, key=functools.cmp_to_key(tcmp))
上一篇 下一篇

猜你喜欢

热点阅读