如何按数字部分的升序对字符串列表进行排序

How can I sort a list of strings in ascending order of numeric part

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

'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'

有谁知道这是如何实现的?

下面按所有字符串的部分排序列表,其中连续数字部分被视为int,其他为[=14] =]:

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']

另请注意,以上将根据字符串中找到的所有部分 进行排序(包括数字和 non-numerical)。这是为了符合:“(...) 有多个数字部分”。

例如:

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

我相信你的数据总是喜欢这种格式。

lst = ['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']

key =lambda e:int(e.split('\')[-1].split('mm.')[0])
print(sorted(lst,key=key))

输出

['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']

您可以使用名为 natsort 的库进行排序

from natsort import natsorted

lst = ['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']

natsorted(lst)

这个Returns:

C:\Python\Python310\Scripts\mockup_testmm.JPG
C:\Python\Python310\Scripts\mockup_testmm.JPG
C:\Python\Python310\Scripts\mockup_testmm.JPG
C:\Python\Python310\Scripts\mockup_testmm.JPG
C:\Python\Python310\Scripts\mockup_testmm.JPG
C:\Python\Python310\Scripts\mockup_testmm.JPG
C:\Python\Python310\Scripts\mockup_testmm.JPG
C:\Python\Python310\Scripts\mockup_testmm.JPG
C:\Python\Python310\Scripts\mockup_testmm.JPG
C:\Python\Python310\Scripts\mockup_testmm.JPG
C:\Python\Python310\Scripts\mockup_testmm.JPG
C:\Python\Python310\Scripts\mockup_testmm.JPG
C:\Python\Python310\Scripts\mockup_testmm.JPG

l.sort()支持自定义排序功能。您可以通过以下代码片段实现您的预​​期结果。

l = ['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']
l.sort(key=lambda x: int(x.split('\')[-1][0:-6]))
print(l)