How to sort the outer and inner sublist of a nested list in Python?

前端 未结 2 544
忘掉有多难
忘掉有多难 2021-01-15 03:49

First of all, apologies if this too naive (I am a beginner). I have the following type of list of lists, which I would like to first sort by the last member of the inner li

2条回答
  •  难免孤独
    2021-01-15 04:00

    >>> data =  [[1, .45, 0], [2, .49, 2], [3, .98, 0], [4, .82, 1], [5, .77, 1], [6, .98, 2] ]
    >>> sorted(data, key=lambda x:(x[2], -x[1]))
    [[3, 0.98, 0], [1, 0.45, 0], [4, 0.82, 1], [5, 0.77, 1], [6, 0.98, 2], [2, 0.49, 2]]
    

    You can add extra terms to the lambda as required

提交回复
热议问题