gpt4 book ai didi

python - 对 datetime.datetime 二维数组进行排序

转载 作者:行者123 更新时间:2023-12-01 09:06:52 25 4
gpt4 key购买 nike

我有以下二维数组,日期格式为 %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 结尾,它不会记录秒数。那么如何按日期对这个数组进行排序呢?

最佳答案

假设您拥有的是一个列表,如@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])

关于python - 对 datetime.datetime 二维数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51970737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com