根据日期对列表排序

2024-09-27 00:13:37 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我编写的一个代码,用于根据某些参数对列表中的元素进行排序。其中一个参数是日期。现在日期格式是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

Tags: import元素参数格式element解决方案sortdd
2条回答

对于Python,对以下列表进行排序

[('2011','03','12'),('2011','02','12'),('2011','02','10'),('2009','01','25']

或者这个

^{pr2}$

是等价的。在

我提议:

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]

试试这个:

sorted(List, key=lambda x: (x[2].split('/')[2], x[2].split('/')[0], x[2].split('/')[1]))

例如:

^{pr2}$

它返回:

[['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'))
...

相关问题 更多 >

    热门问题