Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren0
Lambda Code Logic
Could anyone let me know what lower() do here? Also how this code sorted the list? https://code.sololearn.com/cyGB02ZHLAFT/?ref=app
2 Antworten
+ 3
Assume that list have names like ' firstName secondName' so it splitting this name to 2 parts by space then converting last(secondName) one to lower case and this is a key now to sort items..
'Alf Zed' , 'Mike Mo' , 'Steve Aardvark'
This result to by lambda is
'zeb, mo, aradvark' this gets sorted to 'aradvark, mo, zed'.. now it's corresponding original values gets this order..
+ 2
Also... the line below is a bit unnecessary:
names.sort(key=lambda x: x.split()[-1].lower())
It could be simplified as:
names.sort(key=lambda x: x.split()[1])
The .lower() method isn't necessary here and neither is the reverse index.