Python beginner - Sorting tuples using lambda functions

后端 未结 3 1594
生来不讨喜
生来不讨喜 2021-01-15 03:05

I\'m going through the tutorial intro for Python and I\'m stuck on understanding a piece of code. This is from section 4.7.5 of the tutorial.

pairs = [(1, \'         


        
3条回答
  •  梦毁少年i
    2021-01-15 03:35

    your lambda function takes an tuple as input and return the element with index 1 (so the second element since the first would have 0 for index). so the sorting will only take into consideration the second element of each tuple (the English word). That's why your output is sorted alphabetically in the second element 'four'>'one'>'three'>'two'

提交回复
热议问题