简体   繁体   English

在python 3.3中按日期排序2d数组

[英]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. 我有3d的2d数组,并且在它们里面是一个字符串,它是一个日期,我想按它们中的那个日期对它们进行排序。 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 如果出于某种原因您反对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 我建议在常规时间模块上使用datetime的原因是datetime可以在Ive测试的将来看到……而time模块只能工作到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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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