Sorting a Python list by two fields

前端 未结 7 1036
不思量自难忘°
不思量自难忘° 2020-11-22 07:51

I have the following list created from a sorted csv

list1 = sorted(csv1, key=operator.itemgetter(1))

I would actually like to sort the list

7条回答
  •  悲&欢浪女
    2020-11-22 08:48

    No need to import anything when using lambda functions.
    The following sorts list by the first element, then by the second element.

    sorted(list, key=lambda x: (x[0], -x[1]))
    

提交回复
热议问题