使用数字对 python 字符串列表进行排序
Sort a python list of strings with a numeric number
我有一个名为 filelist
的文件名列表
In []: filelist
Out []: ['C:\Mon20412\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx',
'C:\Mon25312\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx',
'C:\Mon20362\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx']
我想根据粗体位置的数值文件列表排序
C:\Mon20412\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx
C:\Mon25312\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx
C:\Mon20362\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx
所以在这个例子中,输出将是
Out []: ['C:\Mon20412\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx',
'C:\Mon20362\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx'
'C:\Mon25312\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx']
谢谢!
找到一种好的、可靠的方法来提取您想要的号码。然后使用 key
参数按该数字排序。这对于您的输入来说似乎足够可靠,但效率不高。
a = ['C:\Mon20412\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx',
'C:\Mon25312\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx',
'C:\Mon20362\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx']
def k(a):
x = a.split("\")[-1].split("-")[3]
y = filter(lambda x: x in "0123456789", x)
return int("".join(list(y)))
print(sorted(a, key=k))
输出:
['C:\Mon20412\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx',
'C:\Mon20362\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx',
'C:\Mon25312\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx']
使用正则表达式解析数字并将其用作排序键。
快速而肮脏:
import re
l = ['C:\Mon20412\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx',
'C:\Mon25312\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx',
'C:\Mon20362\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx']
def get_sort_number(s):
pattern = r'C:\Mon\d+\P-2NODE-RAID6-(\d+)'
try:
return int(re.match(pattern, s).group(1))
except AttributeError:
return 0
sorted(l, key=get_sort_number)
这给出了
['C:\Mon20412\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx',
'C:\Mon20362\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx',
'C:\Mon25312\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx']
正则表达式无法匹配的所有字符串都将位于排序列表的开头。
import re
f = lambda s: int(re.findall(r'.*RAID6-(\d+).*', s)[0])
sorted(l, key=f)
我有一个名为 filelist
的文件名列表 In []: filelist
Out []: ['C:\Mon20412\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx',
'C:\Mon25312\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx',
'C:\Mon20362\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx']
我想根据粗体位置的数值文件列表排序
C:\Mon20412\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx
C:\Mon25312\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx
C:\Mon20362\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx
所以在这个例子中,输出将是
Out []: ['C:\Mon20412\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx',
'C:\Mon20362\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx'
'C:\Mon25312\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx']
谢谢!
找到一种好的、可靠的方法来提取您想要的号码。然后使用 key
参数按该数字排序。这对于您的输入来说似乎足够可靠,但效率不高。
a = ['C:\Mon20412\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx',
'C:\Mon25312\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx',
'C:\Mon20362\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx']
def k(a):
x = a.split("\")[-1].split("-")[3]
y = filter(lambda x: x in "0123456789", x)
return int("".join(list(y)))
print(sorted(a, key=k))
输出:
['C:\Mon20412\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx',
'C:\Mon20362\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx',
'C:\Mon25312\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx']
使用正则表达式解析数字并将其用作排序键。
快速而肮脏:
import re
l = ['C:\Mon20412\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx',
'C:\Mon25312\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx',
'C:\Mon20362\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx']
def get_sort_number(s):
pattern = r'C:\Mon\d+\P-2NODE-RAID6-(\d+)'
try:
return int(re.match(pattern, s).group(1))
except AttributeError:
return 0
sorted(l, key=get_sort_number)
这给出了
['C:\Mon20412\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx',
'C:\Mon20362\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx',
'C:\Mon25312\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx']
正则表达式无法匹配的所有字符串都将位于排序列表的开头。
import re
f = lambda s: int(re.findall(r'.*RAID6-(\d+).*', s)[0])
sorted(l, key=f)