How to sort a list of lists by a specific index of the inner list?

后端 未结 11 2519
醉酒成梦
醉酒成梦 2020-11-22 10:39

I have a list of lists. For example,

[
[0,1,\'f\'],
[4,2,\'t\'],
[9,4,\'afsd\']
]

If I wanted to sort the outer list by the string field o

11条回答
  •  孤城傲影
    2020-11-22 11:14

    in place

    >>> l = [[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
    >>> l.sort(key=lambda x: x[2])
    

    not in place using sorted:

    >>> sorted(l, key=lambda x: x[2])
    

提交回复
热议问题