如何在python 2.4上对以下日期数组进行排序
timestamps = ['2011-06-2', '2011-08-05', '2011-02-04', '2010-1-14', '2010-12-13', '2010-1-12', '2010-2-11', '2010-2-07', '2010-12-02', '2011-11-30', '2010-11-26', '2010-11-23', '2010-11-22', '2010-11-16']
答案 0 :(得分:40)
>>> import datetime
>>> dates = [datetime.datetime.strptime(ts, "%Y-%m-%d") for ts in timestamps]
>>> dates.sort()
>>> sorteddates = [datetime.datetime.strftime(ts, "%Y-%m-%d") for ts in dates]
>>> sorteddates
['2010-01-12', '2010-01-14', '2010-02-07', '2010-02-11', '2010-11-16', '2010-11-
22', '2010-11-23', '2010-11-26', '2010-12-02', '2010-12-13', '2011-02-04', '2011
-06-02', '2011-08-05', '2011-11-30']
答案 1 :(得分:21)
sorted(timestamps, key=lambda d: map(int, d.split('-')))
答案 2 :(得分:6)
>>> import time
>>> timestamps = ['2011-06-2', '2011-08-05', '2011-02-04', '2010-1-14', '2010-12-13', '2010-1-12', '2010-2-11', '2010-2-07', '2010-12-02', '2011-11-30', '2010-11-26', '2010-11-23', '2010-11-22', '2010-11-16']
>>> timestamps.sort(key=lambda x: time.mktime(time.strptime(x,"%Y-%m-%d")))
>>> timestamps
['2010-1-12', '2010-1-14', '2010-2-07', '2010-2-11', '2010-11-16', '2010-11-22', '2010-11-23', '2010-11-26', '2010-12-02', '2010-12-13', '2011-02-04', '2011-06-2', '2011-08-05', '2011-11-30']
答案 3 :(得分:5)
这样做:
timestamps.sort()
结果:
['2010-1-12',
'2010-1-14',
'2010-11-16',
'2010-11-22',
'2010-11-23',
'2010-11-26',
'2010-12-02',
'2010-12-13',
'2010-2-07',
'2010-2-11',
'2011-02-04',
'2011-06-2',
'2011-08-05',
'2011-11-30']
订单年 - 月 - 日允许进行这样的排序,因为一个月之前的一天变化,一个月在时间过去的一年之前发生变化。
就像一个数字:统一数字(最右边的数字)在十位数之前改变,后者在百位数之前改变,当逐渐增加1时。
事实上 sort()从左到右处理:如果一个精确位置的字符在两个字符串中相同以进行排序,它将检查两个字符中的两个字符在以下位置的字符串,以决定哪一个在逻辑上先于。
此外'0'< '1'是 True ,'1'< '2'是 True 等
答案 4 :(得分:1)
map(lambda x:x[1], sorted(map(lambda a:[map(int,a.split('-')),a], timestamps)))
['2010-1-12',
'2010-1-14',
'2010-2-07',
'2010-2-11',
'2010-11-16',
'2010-11-22',
'2010-11-23',
'2010-11-26',
'2010-12-02',
'2010-12-13',
'2011-02-04',
'2011-06-2',
'2011-08-05',
'2011-11-30']
答案 5 :(得分:1)
print(sorted(list_of_strings,key=lambda x :(int(x.split('-')[0]),int(x.split('-')[1]),int(x.split('-')[2])))
答案 6 :(得分:0)
如果您将它们分类为相同的格式,则只需拨打timestamps.sort()
答案 7 :(得分:0)
这种升序日期为dd / mm / yyyy格式
convert image.png -alpha off -colorspace gray grayimage.png
convert pattern.png -fuzz 20% -trim +repage trimpattern.png
convert -size 200x152 tile:trimpattern.png tiledpattern.png
convert grayimage.png tiledpattern.png -compose multiply -composite multipliedimage.png
convert grayimage.png -fuzz 2% -transparent white -alpha extract alphaimage.png
convert multipliedimage.png alphaimage.png -alpha off -compose copy_opacity -composite -compose over -background white -flatten result2.png
答案 8 :(得分:0)
在Python 3中并使用(我个人最喜欢的)理解。对于日期,我将始终使用Python的+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---+
|plan |tag|
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---+
|== Physical Plan ==
*(1) Project [unique_id#27L]
+- *(1) Filter (true = (exists#53 && exists#54))
+- *(1) BroadcastHashJoin [country#22], [country#3], ExistenceJoin(exists#54), BuildRight
:- *(1) BroadcastHashJoin [country#22], [country#8], ExistenceJoin(exists#53), BuildRight
: :- *(1) Project [country#22, unique_id#27L]
: : +- *(1) Filter isnotnull(EMAIL#20)
: : +- LocalTableScan [name#19, email#20, phone#21, country#22, unique_id#27L]
: +- BroadcastExchange HashedRelationBroadcastMode(List(input[0, string, true]))
: +- LocalTableScan [country#8]
+- BroadcastExchange HashedRelationBroadcastMode(List(input[0, string, true]))
+- LocalTableScan [country#3]|big|
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---+
lib:
datetime