有如下二维列表:
l2 = [["2011-1-1",23],["2011-1-1",34],["2012-1-2",13],["2011-1-1",12],["2012-1-2",12]]
排序方式:
from operator import itemgetter
# 日期主排序关键字,分数为次排序关键字,均为升序
l2 = [["2011-1-1",23],["2011-1-1",34],["2012-1-2",13],["2011-1-1",12],["2012-1-2",12]]
l2.sort(key=itemgetter(0,1))
print(l2)
# 日期升序,分数降序
l2 = [["2011-1-1",23],["2011-1-1",34],["2012-1-2",13],["2011-1-1",12],["2012-1-2",12]]
l2.sort(key=itemgetter(1), reverse=True)
l2.sort(key=itemgetter(0))
print(l2)
结果:
[['2011-1-1', 12], ['2011-1-1', 23], ['2011-1-1', 34], ['2012-1-2', 12], ['2012-1-2', 13]]
[['2011-1-1', 34], ['2011-1-1', 23], ['2011-1-1', 12], ['2012-1-2', 13], ['2012-1-2', 12]]