下面是我编写的一个代码,用于根据某些参数对列表中的元素进行排序。其中一个参数是日期。现在日期格式是mm/dd/yy。现在如果年份相同,我就不会面临任何问题。但是,如果我将某个元素的年份从2011年改为2012年,那么解决方案就会失效。例如,它将2012年3月15日视为2011年3月18日之前。我怎样才能克服这一切。我知道将日期格式更改为yy/mm/dd将给出正确的解决方案,但我不想修改它。Plz建议一条出路。在
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
对于Python,对以下列表进行排序
或者这个
^{pr2}$是等价的。在
我提议:
使用strptime()可以显著提高执行时间,执行时间是原来的16倍:
结果
试试这个:
例如:
^{pr2}$它返回:
甚至更好(前提是你做到了
import datetime
):相关问题 更多 >
编程相关推荐