how to sort following python list
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"
]
to following
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",
]
can it be possible to make it single liner
sort the list and pass a key that splits the strings in the list on '='
and picks the numeric part, nlist.sort
modifies the original list, if you want a new list you're better off with sorted()
nlist.sort(key=lambda x: int(x.split('=')[0]))
print(nlist)
Output
['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']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.