我有一个列表列表,其中每个子列表都包含日期值。如何根据日期对它进行排序?
tList=[['xX0001', '10006', '102', '', '2018-02-02', 3233.9, 0.0, 36816.18, ''],
['xX0001', '10006', '102', '', '2018-02-01', 4142.45, 0.0, 40146.55, ''],
['xX0001', '10006', '200', '', '2018-02-02', 14367.539999999999, 0.0, 41496.42999999999, ''],
['xX0001', '10006', '200', '', '2018-02-01', 12663.27, 0.0, 56043.94, '']]
答案 0 :(得分:3)
尝试,您会得到期望的排序
In [51]: a=[['xX0001', '10006', '102', '', '2018-02-02', 3233.9, 0.0, 36816.18, ''],
...: ['xX0001', '10006', '102', '', '2018-02-01', 4142.45, 0.0, 40146.55, ''],
...: ['xX0001', '10006', '200', '', '2018-02-02', 14367.539999999999, 0.0, 41496.42999999999, ''],
...: ['xX0001', '10006', '200', '', '2018-02-01', 12663.27, 0.0, 56043.94, '']]
In [52]: sorted(a, key=lambda b: b[4])
Out[52]:
[['xX0001', '10006', '102', '', '2018-02-01', 4142.45, 0.0, 40146.55, ''],
['xX0001', '10006', '200', '', '2018-02-01', 12663.27, 0.0, 56043.94, ''],
['xX0001', '10006', '102', '', '2018-02-02', 3233.9, 0.0, 36816.18, ''],
['xX0001',
'10006',
'200',
'',
'2018-02-02',
14367.539999999999,
0.0,
41496.42999999999,
'']]
In [53]: sorted(a, key=lambda b: b[4])
答案 1 :(得分:1)
尝试:
print(sorted(tList,key=lambda x: x[4]))
或者:
tList.sort(key=lambda x: x[4])
print(tList)