Python - sorting a list -
this question has answer here:
i started playing list , know.
if have data in list in format ['john 1', 'jack 2']
how sort them number?
you can pass key
sorted
function extracts number splitting on space , converts integer.
a = ['john 2', 'jack 1'] print(sorted(a, key=lambda x:int(x.split(' ')[-1]))) # ['jack 1', 'john 2']
Comments
Post a Comment