用日期字符串对列表进行排序python

时间:2018-08-30 13:37:30

标签: python

我正在尝试使用a.sort(key=lambda x: x[0])a.sort()对列表列表进行排序。我约会的第一个元素是日期,我需要对其进行排序。

a = [['1/8/2018', 71], ['10/8/2018', 76], ['11/8/2018', 6], ['12/8/2018', 60], ['13/8/2018', 81], ['14/8/2018', 74], ['15/8/2018', 7], ['16/8/2018', 77], ['17/8/2018', 81], ['18/8/2018', 69], ['19/8/2018', 66], ['2/8/2018', 81], ['20/8/2018', 74], ['21/8/2018', 88], ['22/8/2018', 92], ['23/8/2018', 90], ['24/8/2018', 84], ['25/8/2018', 66], ['26/8/2018', 64], ['27/8/2018', 82], ['28/8/2018', 82], ['29/8/2018', 79], ['3/8/2018', 8], ['4/8/2018', 64], ['5/8/2018', 64], ['6/8/2018', 12], ['7/8/2018', 8], ['8/8/2018', 83], ['9/8/2018', 77]]

我需要的结果:

[['1/8/2018', 71],['2/8/2018', 81], ['3/8/2018', 8], ['4/8/2018', 64], ['5/8/2018', 64], ['6/8/2018', 12],['7/8/2018', 8], ['8/8/2018', 83], ['9/8/2018', 77], ['10/8/2018', 76], ['11/8/2018', 6], ['12/8/2018', 60], ['13/8/2018', 81], ['14/8/2018', 74], ['15/8/2018', 7], ['16/8/2018', 77], ['17/8/2018', 81], ['18/8/2018', 69], ['19/8/2018', 66], ['20/8/2018', 74], ['21/8/2018', 88], ['22/8/2018', 92], ['23/8/2018', 90], ['24/8/2018', 84], ['25/8/2018', 66], ['26/8/2018', 64], ['27/8/2018', 82], ['28/8/2018', 82], ['29/8/2018', 79]]

2 个答案:

答案 0 :(得分:3)

使用datetime.datetime.strptime

例如:

import datetime
a = [['1/8/2018', 71], ['10/8/2018', 76], ['11/8/2018', 6], ['12/8/2018', 60], ['13/8/2018', 81], ['14/8/2018', 74], ['15/8/2018', 7], ['16/8/2018', 77], ['17/8/2018', 81], ['18/8/2018', 69], ['19/8/2018', 66], ['2/8/2018', 81], ['20/8/2018', 74], ['21/8/2018', 88], ['22/8/2018', 92], ['23/8/2018', 90], ['24/8/2018', 84], ['25/8/2018', 66], ['26/8/2018', 64], ['27/8/2018', 82], ['28/8/2018', 82], ['29/8/2018', 79], ['3/8/2018', 8], ['4/8/2018', 64], ['5/8/2018', 64], ['6/8/2018', 12], ['7/8/2018', 8], ['8/8/2018', 83], ['9/8/2018', 77]]
a.sort(key=lambda x: datetime.datetime.strptime(x[0], "%d/%m/%Y"))
print(a)

输出:

[['1/8/2018', 71], ['2/8/2018', 81], ['3/8/2018', 8], ['4/8/2018', 64], ['5/8/2018', 64], ['6/8/2018', 12], ['7/8/2018', 8], ['8/8/2018', 83], ['9/8/2018', 77], ['10/8/2018', 76], ['11/8/2018', 6], ['12/8/2018', 60], ['13/8/2018', 81], ['14/8/2018', 74], ['15/8/2018', 7], ['16/8/2018', 77], ['17/8/2018', 81], ['18/8/2018', 69], ['19/8/2018', 66], ['20/8/2018', 74], ['21/8/2018', 88], ['22/8/2018', 92], ['23/8/2018', 90], ['24/8/2018', 84], ['25/8/2018', 66], ['26/8/2018', 64], ['27/8/2018', 82], ['28/8/2018', 82], ['29/8/2018', 79]]

答案 1 :(得分:2)

您可以使用str.split

a = [['1/8/2018', 71], ['10/8/2018', 76], ['11/8/2018', 6], ['12/8/2018', 60], ['13/8/2018', 81], ['14/8/2018', 74], ['15/8/2018', 7], ['16/8/2018', 77], ['17/8/2018', 81], ['18/8/2018', 69], ['19/8/2018', 66], ['2/8/2018', 81], ['20/8/2018', 74], ['21/8/2018', 88], ['22/8/2018', 92], ['23/8/2018', 90], ['24/8/2018', 84], ['25/8/2018', 66], ['26/8/2018', 64], ['27/8/2018', 82], ['28/8/2018', 82], ['29/8/2018', 79], ['3/8/2018', 8], ['4/8/2018', 64], ['5/8/2018', 64], ['6/8/2018', 12], ['7/8/2018', 8], ['8/8/2018', 83], ['9/8/2018', 77]]
new_a = sorted(a, key=lambda x:[int(i) for i in x[0].split('/')])

输出:

[['1/8/2018', 71], ['2/8/2018', 81], ['3/8/2018', 8], ['4/8/2018', 64], ['5/8/2018', 64], ['6/8/2018', 12], ['7/8/2018', 8], ['8/8/2018', 83], ['9/8/2018', 77], ['10/8/2018', 76], ['11/8/2018', 6], ['12/8/2018', 60], ['13/8/2018', 81], ['14/8/2018', 74], ['15/8/2018', 7], ['16/8/2018', 77], ['17/8/2018', 81], ['18/8/2018', 69], ['19/8/2018', 66], ['20/8/2018', 74], ['21/8/2018', 88], ['22/8/2018', 92], ['23/8/2018', 90], ['24/8/2018', 84], ['25/8/2018', 66], ['26/8/2018', 64], ['27/8/2018', 82], ['28/8/2018', 82], ['29/8/2018', 79]]