gpt4 book ai didi

python - 每小时桶的最小和最大时间戳

转载 作者:行者123 更新时间:2023-11-30 23:02:55 24 4
gpt4 key购买 nike

我有一个带有时间戳的文本文件。

示例:

16-07-2015 18:08:20
16-07-2015 18:08:22
16-07-2015 18:08:30
16-07-2015 18:08:40
17-07-2015 10:04:01
17-07-2015 10:14:31
17-07-2015 10:14:59
17-07-2015 12:24:11
....

现在我需要每小时的最小值和最大值,如下例所示。

示例:

16-07-2015 18:08:20 - 16-07-2015 18:08:40
17-07-2015 10:04:01 - 17-07-2015 10:14:59
17-07-2015 12:24:11 - ....

我怎样才能实现这个目标?

最佳答案

如果您有一个可迭代的 datetime 对象,您可以按天和小时对它们进行分组,然后使用 itertools.groupby() 查找其中的第一个和最后一个。 :

from itertools import groupby

def min_max_per_hour(iterable):
for dayhour, grouped in groupby(iterable, lambda dt: (dt.date(), dt.hour)):
minimum = next(grouped) # first object is the minimum for this hour
maximum = minimum # starting value
for dt in grouped:
maximum = dt # last assignment is the maximum within this hour
yield (minimum, maximum)

这依赖于包含按排序顺序datetime对象的迭代。

要生成输入可迭代对象,请在生成器表达式或另一个生成器中解析文本文件;无需将所有内容一次性保存在内存中:

from datetime import datetime

with open(input_filename) as inf:
# generator expression
datetimes = (datetime.strptime(line.strip(), '%d-%m-%Y %H:%M:%S') for line in inf)
for mindt, maxdt in min_max_per_hour(datetimes):
print mindt, maxdt

演示:

>>> from datetime import datetime
>>> from itertools import groupby
>>> def min_max_per_hour(iterable):
... for dayhour, grouped in groupby(iterable, lambda dt: (dt.date(), dt.hour)):
... minimum = next(grouped) # first object is the minimum for this hour
... maximum = minimum # starting value
... for dt in grouped:
... maximum = dt # last assignment is the maximum within this hour
... yield (minimum, maximum)
...
>>> textfile = '''\
... 16-07-2015 18:08:20
... 16-07-2015 18:08:22
... 16-07-2015 18:08:30
... 16-07-2015 18:08:40
... 17-07-2015 10:04:01
... 17-07-2015 10:14:31
... 17-07-2015 10:14:59
... 17-07-2015 12:24:11
... '''.splitlines()
>>> datetimes = (datetime.strptime(line.strip(), '%d-%m-%Y %H:%M:%S') for line in textfile)
>>> for mindt, maxdt in min_max_per_hour(datetimes):
... print mindt, maxdt
...
2015-07-16 18:08:20 2015-07-16 18:08:40
2015-07-17 10:04:01 2015-07-17 10:14:59
2015-07-17 12:24:11 2015-07-17 12:24:11

关于python - 每小时桶的最小和最大时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34261931/

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