简体   繁体   中英

sort 2d array by date in python 3.3

I have 3 arrays that are 2d and inside them is a string that is a date i would like to sort these by that date in them. The arrays are all structured like this:

array1 = [[1,'29-04-2013','U11'],[2,'20-05-2013','U11']]
array2 = [[1,'06-05-2013','U13'],[2,'03-06-2013','U13']]
array3 = [[1,'06-03-2013','U15'],[2,'03-07-2013','U15']]

I would like to get them into an array like this:

all = [[1,'06-03-2013','U15'],[1,'29-04-2013','U11'],[1,'06-05-2013','U13'],[2,'20-05-2013','U11'],[2,'03-06-2013','U13'],[2,'03-07-2013','U15']]

I just need some sort of way to approach this as i havent got a clue how i would do it.Thanks for the help in advance

big_array = array1 + array2 + array3
import dateutil.parser as p
print sorted(big_array,key=lambda x: p.parse(x[1]))

if for somereason you are opposed to dateutil.parser

import datetime
print sorted(big_array,key=lambda x:datetime.datetime.strptime(x[1],"%d-%m-%Y")

the reason I reccommend datetime over the regular time module is that datetime can see as far in the future as Ive tested ... while the time module only works up to like 2035

however you can also do it with the time module

import time
print sorted(big_array,key=lambda x:time.strptime(x[1],"%d-%m-%Y")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM