如何按digit = string对字符串列表进行排序

时间:2016-02-16 09:56:55

标签: 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",
] 

是否可以使其成为单线

1 个答案:

答案 0 :(得分:2)

对列表进行排序并传递一个键,用于分割from scrapy.utils.project import get_project_settings ... myS = get_project_settings() myS.set('JOBDIR', "folder") 列表中的字符串并选择数字部分,'='修改原始列表,如果你想要一个新的列表,你最好关闭与nlist.sort

sorted()

输出

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