I have a list of strings that I have to sort and return in descending order but my code is not working. Could you please help?
lst1 = 'Tadashi Takahiro Takao Takashi Takayuki Takehiko Takeo Takeshi Takeshi'
def lineup_students(string):
lst = string.split(' ')
return sorted(lst, key = len, reverse = True)
print lineup_students(lst1)
My output:
['Takahiro', 'Takayuki', 'Takehiko', 'Tadashi', 'Takashi', 'Takeshi', 'Takeshi', 'Takao', 'Takeo']
Expected output:
['Takehiko','Takayuki','Takahiro','Takeshi', 'Takeshi', 'Takashi','Tadashi','Takeo','Takao']
This question is not a duplicate of how to sort by length of string followed by alphabetical order? or Python Sort List : sadly, neither the first nor the second have helped me to solve my problem (but if I missed something please let me know)
@Edit1: Thanks to Mike and tobspr that gave me part of the solution:
def lineup_students(string):
lst = string.split(' ')
return sorted(lst, key=lambda x: (len(x), x), reverse=True)
def lineup_students(string):
lst = string.split(' ')
lst.sort(reverse=True)
lst.sort(key=len, reverse=True)
return lst
The point is that the code seems not to work with this list of names:
['Shigekazu', 'Takeshi', 'Senichi', 'Ryuichi', 'Yoshio', 'Toshio', 'Noboru',
'Mitsuo', 'Rafu', '']
should equal
['Shigekazu', 'Takeshi', 'Senichi', 'Ryuichi', 'Yoshio', 'Toshio', 'Noboru',
'Mitsuo', 'Rafu']
@edit2: This answer seems to work:
def lineup_students(s):
return sorted(s.split(), key=lambda i:(len(i),i), reverse=True)