How do I sort the list below?
My results was:
['10','11','12','7','8','9'],['1','2','3','4','5','6']]
Here's my code
lst= [['7','10','8','9','11','12'],['3','1','2','4','6','5']]
for i in lst:
i.sort()
print(i)
Use sorted(list, key=int):
lst= [['7','10','8','9','11','12'],['3','1','2','4','6','5']]
for i in lst:
i = sorted(i, key=int)
print(i)
These are string objects, for string:
'10' < '7' because `1 < 7`
I think you want:
>>> [sorted(map(int, i)) for i in lst]
[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]
>>>
Just covert them to int objects before sort.
If you need keep them in string object, covert them back or set a key:
>>> [list(map(str, i)) for i in [sorted(map(int, i)) for i in lst]]
[['7', '8', '9', '10', '11', '12'], ['1', '2', '3', '4', '5', '6']]
>>> [sorted(i, key=lambda x: int(x)) for i in lst]
[['7', '8', '9', '10', '11', '12'], ['1', '2', '3', '4', '5', '6']]
lst[0]and to access the otherlst[1]... I leave the rest for you.