Reputation:
I have a list of string containing numbers like this:
['line_1.jpg Word930\n', 'line_10.jpg Word203\n', 'line_2.jpg Word31\n', 'line_100.jpg Word7\n', 'line_3.jpg Word60\n', 'line_4.jpg Word52\n']
I want to sort the numbers in the string before the space in each string from small to large,I want this result:
['line_1.jpg Word930\n', 'line_2.jpg Word31\n', 'line_3.jpg Word60\n',
'line_4.jpg Word52\n', 'line_10.jpg Word203\n', 'line_100.jpg Word7\n']
I use this code to sort list:
myList=['line_1.jpg Word930\n', 'line_10.jpg Word203\n', 'line_2.jpg Word31\n', 'line_100.jpg Word7\n', 'line_3.jpg Word60\n', 'line_4.jpg Word52\n']
myList.sort()
print(myList)
But the result is:
['line_1.jpg Word930\n', 'line_10.jpg Word203\n', 'line_100.jpg Word7\n', 'line_2.jpg Word31\n', 'line_3.jpg Word60\n', 'line_4.jpg Word52\n']
This is not right,how to fix the code?
Upvotes: 0
Views: 87
Reputation: 26315
We could split the string on "."
to get the filename, split again on "_"
, then get the number from the last item and convert to an integer. Then we can pass this to the key function of sorted()
.
lst = ['line_1.jpg Word930\n', 'line_2.jpg Word31\n', 'line_3.jpg Word60\n', 'line_4.jpg Word52\n', 'line_10.jpg Word203\n', 'line_100.jpg Word7\n']
print(sorted(lst, key=lambda x: int(x.split(".")[0].split("_")[-1])))
# ['line_1.jpg Word930\n', 'line_2.jpg Word31\n', 'line_3.jpg Word60\n', 'line_4.jpg Word52\n', 'line_10.jpg Word203\n', 'line_100.jpg Word7\n']
Upvotes: 1
Reputation: 4472
You can sorted the list like
myList.sort(key=lambda x: int(x[x.find("_")+ 1:x.find(".")]))
That will sort the list base on the number of the file.
Upvotes: 1
Reputation: 6590
You can use re
to get the value and use that in the list.sort
's keyfunction like,
>>> import re
>>> x
['line_1.jpg Word930\n', 'line_10.jpg Word203\n', 'line_2.jpg Word31\n', 'line_100.jpg Word7\n', 'line_3.jpg Word60\n', 'line_4.jpg Word52\n']
>>> x.sort(key=lambda x: int(re.search(r'line_(\d+).*', x).group(1)))
>>> x
['line_1.jpg Word930\n', 'line_2.jpg Word31\n', 'line_3.jpg Word60\n', 'line_4.jpg Word52\n', 'line_10.jpg Word203\n', 'line_100.jpg Word7\n']
>>>
Upvotes: 2