根据日期排序列表
下面是我写的一段代码,用来根据一些参数对列表中的元素进行排序。其中一个参数是日期。日期的格式是 mm/dd/yy。现在,如果年份相同,我不会遇到任何问题。但是,如果我把某个元素的年份从2011改成2012,程序就出问题了。例如,它会把03/15/2012当作比03/18/2011更早的日期。请问我该怎么解决这个问题。我知道如果把日期格式改成yy/mm/dd就能得到正确的结果,但我不想去改这个格式。请给我一些建议。
from operator import itemgetter
import datetime
List=[['G1','E','03/12/2011',2],
['G2','E','03/10/2011',2],
['G3','2','03/19/2011',1],
['G4','2','03/15/2011',2],
['G6','2','03/15/2011',2]]
print List
List_expedite=[]
for element in List:
if element[1]=='E':
List_expedite.append(element)
print "Expedite List", List_expedite
List_non_expedite=[]
for element in List:
if element[1]!='E':
List_non_expedite.append(element)
print "Non-expedite List", List_non_expedite
List_expedite_sort=sorted(List_expedite, key=itemgetter(-1,-2))
print "Sorted expedite List",List_expedite_sort
List_non_expedite_sort=sorted(List_non_expedite,
key=lambda x: (x[-1],-int(x[-3]),x[-2]))
print "Sorted non-expedite List", List_non_expedite_sort
2 个回答
0
对于Python来说,排序下面这个列表
[('2011','03','12'),('2011','02','12'),('2011','02','10'),('2009','01','25']
或者这个列表
[('2011','03/12'),('2011','02/12'),('2011','02/10'),('2009','01/25']
是一样的。
我建议:
List_expedite = []
List_non_expedite = []
for element in li:
(List_expedite if element[1]=='E' else List_non_expedite).append(element)
List_expedite.sort(key = lambda x: (x[-1], x[-2].rsplit('/',1)[::-1]))
List_non_expedite.sort(key = lambda x: (x[-1], -int(x[-3]), x[-2].rsplit('/',1)[::-1]))
使用strptime()会大大增加执行时间,差不多要长16倍:
from time import clock,strptime
List = [['G1','E','03/12/2011',2],
['Ga','E','01/25/2009',2],
['Gc','E','02/11/2008',2],
['Ge','E','01/02/2007',5],
['G2','E','03/10/2011',2],
['G3','2','03/19/2011',1],
['Gb','E','01/24/2009',2],
['G4','2','03/15/2011',2],
['G6','2','03/15/2011',2],
['Gd','E','02/12/2011',2],]
te = clock()
for i in xrange(1000):
List_expedite = []
List_non_expedite = []
for element in List:
(List_expedite if element[1]=='E' else List_non_expedite).append(element)
List_expedite.sort(key = lambda x: (x[-1], strptime(x[-2],'%m/%d/%Y')))
List_non_expedite.sort(key = lambda x: (x[-1], -int(x[-3]), strptime(x[-2],'%m/%d/%Y')))
print clock()-te
print "Expedite List\n", '\n'.join(map(repr,List_expedite))
print
print "Non-expedite List\n", '\n'.join(map(repr,List_non_expedite))
print
te = clock()
for i in xrange(1000):
List_expedite = []
List_non_expedite = []
for element in List:
(List_expedite if element[1]=='E' else List_non_expedite).append(element)
List_expedite.sort(key = lambda x: (x[-1], x[-2].rsplit('/',1)[::-1]))
List_non_expedite.sort(key = lambda x: (x[-1], -int(x[-3]), x[-2].rsplit('/',1)[::-1]))
print clock()-te
print "Expedite List\n", '\n'.join(map(repr,List_expedite))
print
print "Non-expedite List\n", '\n'.join(map(repr,List_non_expedite))
print
结果
3.18868152237
Expedite List
['Gc', 'E', '02/11/2008', 2]
['Gb', 'E', '01/24/2009', 2]
['Ga', 'E', '01/25/2009', 2]
['Gd', 'E', '02/12/2011', 2]
['G2', 'E', '03/10/2011', 2]
['G1', 'E', '03/12/2011', 2]
['Ge', 'E', '01/02/2007', 5]
Non-expedite List
['G3', '2', '03/19/2011', 1]
['G4', '2', '03/15/2011', 2]
['G6', '2', '03/15/2011', 2]
0.199834057122
Expedite List
['Gc', 'E', '02/11/2008', 2]
['Gb', 'E', '01/24/2009', 2]
['Ga', 'E', '01/25/2009', 2]
['Gd', 'E', '02/12/2011', 2]
['G2', 'E', '03/10/2011', 2]
['G1', 'E', '03/12/2011', 2]
['Ge', 'E', '01/02/2007', 5]
Non-expedite List
['G3', '2', '03/19/2011', 1]
['G4', '2', '03/15/2011', 2]
['G6', '2', '03/15/2011', 2]
11
试试这个:
sorted(List, key=lambda x: (x[2].split('/')[2], x[2].split('/')[0], x[2].split('/')[1]))
举个例子:
List=[['G1','E','03/12/2011',2],
['G2','E','03/10/2011',2],
['G3','2','03/19/2012',1],
['G4','2','03/15/2010',2],
['G6','2','03/15/2012',2]]
它返回:
[['G4', '2', '03/15/2010', 2],
['G2', 'E', '03/10/2011', 2],
['G1', 'E', '03/12/2011', 2],
['G6', '2', '03/15/2012', 2],
['G3', '2', '03/19/2012', 1]]
或者更好一点(前提是你已经做了 import datetime
):
...
sorted(List, key=lambda x: datetime.datetime.strptime(x[2], '%m/%d/%Y'))
...