python - how does lambda pick values to return -
in tutorial, see piece of code verified works: https://wiki.python.org/moin/howto/sorting
>>> student_tuples = [ ('john', 'a', 15), ('jane', 'b', 12), ('dave', 'b', 10), ] >>> sorted(student_tuples, key=lambda student: student[2]) # sort age [('dave', 'b', 10), ('jane', 'b', 12), ('john', 'a', 15)] why student[2] known python refer third element of each tuple? why didn't try sort third tuple, instead of third item of each tuple?
because sorted function sends elements of iterable argument lambda function.and in case tuples sent lambda.
key specifies function of 1 argument used extract comparison key each list element: key=str.lower. default value none (compare elements directly).
Comments
Post a Comment