python如何按数字对字符串列表排序=字符串

2024-04-25 23:01:23 发布

您现在位置:Python中文网/ 问答频道 /正文

如何排序下面的python列表

nlist = [
"494=Deploy\00000001.inx",
"19=Deploy\0000000144.exe",
"2=Deploy\00000001_index.dat",
"9=Deploy\0000001b_index.bin",
"1=Deploy\00000001_index.bin",
"7=Deploy\00000019_index.bin",
"2=Deploy\00000001_Onedata.dat",
"19=Deploy\000000024444.exe"
] 

至以下

sortedList = [
"1=Deploy\00000001_index.bin",
"2=Deploy\00000001_index.dat",
"2=Deploy\00000001_Onedata.dat",
"7=Deploy\00000019_index.bin",
"9=Deploy\0000001b_index.bin",
"19=Deploy\0000000144.exe",
"19=Deploy\000000024444.exe",
"494=Deploy\00000001.inx",
] 

能不能做一个班轮


Tags: 列表indexbin排序exedatdeployonedata
1条回答
网友
1楼 · 发布于 2024-04-25 23:01:23

对列表排序并传递一个键,该键在'='上拆分列表中的字符串并选择数字部分,nlist.sort修改原始列表,如果您想要一个新列表,最好使用sorted()

nlist.sort(key=lambda x: int(x.split('=')[0]))
print(nlist)

输出

['1=Deploy\x0000001_index.bin', '2=Deploy\x0000001_index.dat', '2=Deploy\x0000001_Onedata.dat', '7=Deploy\x0000019_index.bin', '9=Deploy\x000001b_index.bin', '19=Deploy\x000000144.exe', '19=Deploy\x00000024444.exe', '494=Deploy\x0000001.inx']

相关问题 更多 >

    热门问题