Reputation: 5
I have the following list and I need to order them from biggest to smallest names and numbers.
['joe: 5', 'ed: 9', 'joe: 7', 'ed: 8']
I'm having the following problems:
So my end result should be this:
['ed: 9', 'ed: 8', 'joe: 7', 'joe: 5']
Upvotes: 0
Views: 67
Reputation: 251106
>>> lis=['joe: 5', 'ed: 9', 'joe: 7', 'ed: 8']
>>> sorted(lis,key=lambda x:int(x.split()[-1]),reverse=True)
>>> ['ed: 9', 'ed: 8', 'joe: 7', 'joe: 5']
you can fetch the integers in each list item using str.split
(as shown below), and that integer is then used to sort the list:
>>> int(lis[0].split(":")[1])
>>> 5
#or
>>> int(lis[0].split()[1])
>>> 5
Upvotes: 5