根据日期对列表进行排序

时间:2011-04-05 08:11:17

标签: python

下面是我编写的代码,用于根据某些参数对列表元素进行排序。一个参数是日期。现在日期格式为mm / dd / yy。现在,如果年份相同,我不会遇到任何问题。但是,如果我将特定元素的2011年更改为2012年,则解决方案会崩溃。例如,它在03/15/2012之前就像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

2 个答案:

答案 0 :(得分: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'))
...

答案 1 :(得分: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]