Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据日期对列表进行排序_Python - Fatal编程技术网

Python 根据日期对列表进行排序

Python 根据日期对列表进行排序,python,Python,下面是我编写的代码,用于根据某些参数对列表中的元素进行排序。其中一个参数是日期。现在日期格式是mm/dd/yy。如果今年是同一年,我不会面临任何问题。但是,如果我将某个特定元素的年份从2011年更改为2012年,则解决方案将崩溃。例如,它将2012年3月15日视为2011年3月18日之前的日期。我该如何克服这一点。我知道将日期格式更改为yy/mm/dd将给我提供正确的解决方案,但我不想对此进行修补。请建议一条出路 from operator import itemgetter import da

下面是我编写的代码,用于根据某些参数对列表中的元素进行排序。其中一个参数是日期。现在日期格式是mm/dd/yy。如果今年是同一年,我不会面临任何问题。但是,如果我将某个特定元素的年份从2011年更改为2012年,则解决方案将崩溃。例如,它将2012年3月15日视为2011年3月18日之前的日期。我该如何克服这一点。我知道将日期格式更改为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
试试这个:

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]]
或者更好(如果您导入了日期时间):


对于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]))
使用strtime()大大增加了执行时间,比原来长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]

只是澄清一下:据python所知,yiu正在按字符串排序。您需要对该字符串执行一些操作,以使python对其进行排序,或者像上面那样(重新排列数据),或者通过将其转换为日期并对其进行排序。FWIW,第一个可以稍微缩短为
排序(List,key=lambda x:(lambda d=x[2]。split('/'):(d[2],d[0],d[1]))
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]