pythonic way to sort a list of lists by the last item of the inner list

后端 未结 1 852
無奈伤痛
無奈伤痛 2021-01-21 04:20

I have a list like this

[[x,y,1],[w,u,4],[m,n,3] ... [p,q,5]]

I need to sort the outer list by the third (last) element of the inner list, the

相关标签:
1条回答
  • 2021-01-21 05:09
    my_list.sort(key=lambda x: x[-1])
    

    or

    my_list.sort(key=operator.itemgetter(-1))
    

    The second option is slightly faster.

    0 讨论(0)
提交回复
热议问题