+ 2
Can you explain this code?
x=['apple','orange','banana','mango'] x.sort(key=lambda x:x[-1]) print(x) output=['banana','apple','orange','mango'] but how can explain this please.
2 ответов
+ 3
Here you are using key which is a optional parameter of sort function.Key specifies on what basis you want the sorting to be done.Since you have set this to x [-1] using lambda expression,the list is sorted based on the last index of each word.
0
It looks like an String or arrary String to me. You have the variables in the pamater. the next line is calling the SORT method. Then it is using the method LAMBDA stored in key variable through x. -1 is basically the entire String Length. You are then telling it to print X after being modified by LAMBDA.