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

前端 未结 6 2022
离开以前
离开以前 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 06:58

    distances.sort(key=lambda x: x[1])
    

    This is the function. And here x is the list, in which we are adding x[1] i.e 2nd element of list to the sort function. So, basically we are adding every list's 2nd element (i.e x[1]) to the sort function. I hope you understand this.

提交回复
热议问题