排序日期时间。日期时间二维阵列

2024-03-29 09:31:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下2D数组,日期格式为%Y-%m-%d%H:%m:%S:

[['second comment test', datetime.datetime(2018, 8, 22, 15, 49, 40), 's']
['this comment originated in service now', datetime.datetime(2018, 8, 22, 14, 45), 's']
['this is a longer description used for testing', datetime.datetime(2018, 8, 22, 14, 13, 49), 'z']]

我注意到,如果其中一个时间记录正好是一分钟,例如2018-08-22 14:45:00意味着它在00结束,它不会记录秒数。那么我如何按日期订购这个数组呢?你知道吗


Tags: intestdatetimeis格式service记录comment
1条回答
网友
1楼 · 发布于 2024-03-29 09:31:09

假设您拥有的是一个列表,如@DietrichEpp所述,您可以简单地按以下方式对该数组排序:

list_ = [
    ['second comment test', datetime.datetime(2018, 8, 22, 15, 49, 40), 's'],
    ['this comment originated in service now', datetime.datetime(2018, 8, 22, 14, 45), 's'],
    ['this is a longer description used for testing', datetime.datetime(2018, 8, 22, 14, 13, 49), 'z'],]
list_.sort(key=lambda x: x[1])

相关问题 更多 >