我有一个路径列表,其中包含多个数字部分,以下是其中的一部分:

'C:\\Python\\Python310\\Scripts\\mockup_test\\17mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\18mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\19mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\1mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\20mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\21mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\29mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\2mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\30mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\31mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\38mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\39mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\3mm.JPG'

并使用.sort()不会更改它,因为它认为它已被排序.

以下是它应该是什么:

预期结果:

'C:\\Python\\Python310\\Scripts\\mockup_test\\1mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\2mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\3mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\17mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\18mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\19mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\20mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\21mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\29mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\30mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\31mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\38mm.JPG',
'C:\\Python\\Python310\\Scripts\\mockup_test\\39mm.JPG'

有人知道这是如何实现的吗?

推荐答案

下面按all对找到的字符串部分进行排序,其中连续数字部分被视为int,其他部分被视为str:

import re 

def split_str_int(s):
    a = re.split(r'(\d+)', s)
    a[1::2] = map(int, a[1::2])
    return a

newlist = sorted(mylist, key=split_str_int)

关于您的数据:

>>> newlist
['C:\\Python\\Python310\\Scripts\\mockup_test\\1mm.JPG',
 'C:\\Python\\Python310\\Scripts\\mockup_test\\2mm.JPG',
 'C:\\Python\\Python310\\Scripts\\mockup_test\\3mm.JPG',
 'C:\\Python\\Python310\\Scripts\\mockup_test\\17mm.JPG',
 'C:\\Python\\Python310\\Scripts\\mockup_test\\18mm.JPG',
 'C:\\Python\\Python310\\Scripts\\mockup_test\\19mm.JPG',
 'C:\\Python\\Python310\\Scripts\\mockup_test\\20mm.JPG',
 'C:\\Python\\Python310\\Scripts\\mockup_test\\21mm.JPG',
 'C:\\Python\\Python310\\Scripts\\mockup_test\\29mm.JPG',
 'C:\\Python\\Python310\\Scripts\\mockup_test\\30mm.JPG',
 'C:\\Python\\Python310\\Scripts\\mockup_test\\31mm.JPG',
 'C:\\Python\\Python310\\Scripts\\mockup_test\\38mm.JPG',
 'C:\\Python\\Python310\\Scripts\\mockup_test\\39mm.JPG']

还请注意,上面的内容将根据字符串中发现的all个部分(数字和非数字)进行排序.这是为了符合:"(…)有多个数字部分".

例如:

mylist = [
    'ab6cd45',
    'ab6cd2',
    'a6cd3',
    'ab4cd60',
    'a',
]
>>> sorted(mylist, key=split_str_int)
['a', 'a6cd3', 'ab4cd60', 'ab6cd2', 'ab6cd45']

Python相关问答推荐

在Google Colab中设置Llama-2出现问题-加载判断点碎片时Cell-run失败

优化pytorch函数以消除for循环

管道冻结和管道卸载

从dict的列中分钟

为什么sys.exit()不能与subproccess.run()或subprocess.call()一起使用

加速Python循环

使用@ guardlasses. guardlass和注释的Python继承

用NumPy优化a[i] = a[i-1]*b[i] + c[i]的迭代计算

从groupby执行计算后创建新的子框架

如何在Raspberry Pi上检测USB并使用Python访问它?

使用NeuralProphet绘制置信区间时出错

给定高度约束的旋转角解析求解

AES—256—CBC加密在Python和PHP中返回不同的结果,HELPPP

python panda ExcelWriter切换动态公式到数组公式

如何使用pytest在traceback中找到特定的异常

来自Airflow Connection的额外参数

如何在polars group_by中将多个行分组到列表中

Parsel无法访问嵌套元素

torch 二维张量与三维张量欧氏距离的计算

有条件的滚动平均数(面试问题)