繁体   English   中英

python如何按digit = string排序字符串列表

[英]python How to sort list of string by digit=string

如何对以下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",
] 

有可能使它成为单衬里吗

对列表进行排序,并传递一个键,该键将'='上的列表中的字符串分割开并选择数字部分, 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']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM