What does this mean: key=lambda x: x[1] ?

前端 未结 6 2029
离开以前
离开以前 2021-02-05 06:17

I see it used in sorting, but what do the individual components of this line of code actually mean?

key=lambda x: x[1]

What\'s lambda

6条回答
  •  忘了有多久
    2021-02-05 07:00

    student_tuples = [
        ('john', 'A', 15),
        ('jane', 'B', 12),
        ('dave', 'B', 10),]
    sorted(student_tuples, key=lambda student: student[2])   # sort by age
    >>>>[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
    

    from official documentation

提交回复
热议问题