How to sort list by alphanumeric values
a = ['v1_0005.jpg', 'v1_00015.jpg', 'v2_0007.jpg', 'v2_0002.jpg']
while sorting list using a.sort()
I get
['v1_0005.jpg', 'v1_00015.jpg', 'v2_0007.jpg', 'v2_0002.jpg']
which means sorting order is wrong, I expect list is sort by alphanumeric value in python
Expected list to be :
['v2_0002.jpg','v1_0005.jpg','v2_0007.jpg', 'v1_00015.jpg']
You have a problem: your list format is quite wrong - instead of v1_00015.jpg
it should be v1_0015.jpg
(no extra 0
). Without that zero Python built-ins .sort()
and sorted()
work correctly. To transform the list, you can use this:
for i,e in enumerate(a):
while len(a[i]) > 11:
a[i] = e.replace('v1_0', 'v1_')
It replaces v1_0
to v1_
in every a
item till item length == 11
. Then just use a.sort()
:
a.sort()
print(a)
And it prints:
['v1_0001.jpg', 'v1_0002.jpg', 'v1_0003.jpg', 'v1_0015.jpg', 'v1_0017.jpg']
You have to some techniques inorder to get the result that you want, ie you have to write a sorting function with the conditions.
So here you can make use of regex and lambda in python inorder to sort as you wish
import re
def sorted_nicely( l ):
"""
Sorts the given iterable in the way that is expected.
Required arguments:
l -- The iterable to be sorted.
"""
convert = lambda text: int(text) if text.isdigit() else text
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
return sorted(l, key = alphanum_key)
# Driver code
a = ["v1_0001.jpg","v1_0002.jpg","v1_0003.jpg","v1_00017.jpg","v1_00015.jpg"]
print(sorted_nicely(a))
Output
['v1_0001.jpg', 'v1_0002.jpg', 'v1_0003.jpg', 'v1_00015.jpg', 'v1_00017.jpg']
I'm assuming you want to use the numeric values. So you need to parse them out...
This can be done with a one-liner as so -
a = ['v1_0001.jpg', 'v1_00015.jpg', 'v1_00017.jpg', 'v1_0002.jpg', 'v1_0003.jpg']
a.sort(key=lambda x: int(x.split('v1_')[1].split('.jpg')[0]))
print(a)
# ['v1_0001.jpg', 'v1_0002.jpg', 'v1_0003.jpg', 'v1_00015.jpg', 'v1_00017.jpg']
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.