从Python中的时间戳列表中查找每天的时间间隔

时间:2015-02-19 12:14:02

标签: python timestamp python-datetime

我正在尝试从Python中的unix时间戳列表中计算每天的时间间隔。我已经搜索了关于堆栈溢出的类似问题,但大多数都是计算增量或SQL解决方案的例子。

我有一份清单:

timestamps = [1176239419.0, 1176334733.0, 1176445137.0, 1177619954.0, 1177620812.0, 1177621082.0, 1177838576.0, 1178349385.0, 1178401697.0, 1178437886.0, 1178926650.0, 1178982127.0, 1179130340.0, 1179263733.0, 1179264930.0, 1179574273.0, 1179671730.0, 1180549056.0, 1180763342.0, 1181386289.0, 1181990860.0, 1182979573.0, 1183326862.0]

我可以使用以下命令轻松将此时间戳列表转换为datetime对象:

[dt.datetime.fromtimestamp(int(i)) for i in timestamps]

从那里我可以编写相当冗长的代码,其中保留第一天/月,并检查列表中的下一个项目是否是同一天/月。如果是这样的话,我会查看时间,从当天获得第一个和最后一个,并将间隔+日/月存储在字典中。

由于我是Python的新手,我想知道在没有滥用if / else语句的情况下,在这种编程语言中执行此操作的最佳方法是什么。

提前谢谢

3 个答案:

答案 0 :(得分:1)

您可以使用collections.defaultdict。当你在没有对规模和成员进行初步估计的情况下尝试建立一个集合时,它会非常方便。

from collections import defaultdict

# Initialize default dict by the type list
# Accessing a member that doesn't exist introduces that entry with the deafult value for that type
# Here, when accessing a non-existant member adds an empty list to the collection
intervalsByDate = defaultdict(list)

for t in timestamps:
    dt = dt.datetime.fromtimestamp(t)
    myDateKey = (dt.day, dt.month, dt.year)
    # If the key doesn't exist, a new empty list is added
    intervalsByDate[myDateKey].append(t)

从此,intervalsByDate现在是dict,其值为基于日历日期排序的列表时间戳。对于每个日期,您可以对时间戳进行排序并获取总间隔。迭代defaultdictdict相同(是dict s的子类。)

output = {}
for date, timestamps in intervalsByDate.iteritems():
    sortedIntervals = sorted(timestamps)
    output[date] = sortedIntervals[-1] - sortedIntervals[0]

现在output是一个日期地图,其间隔以毫秒为单位。随你做吧!


修改

  

没有订购钥匙是正常的吗?我把不同月份的钥匙混合在一起。

是的,因为(哈希)地图& dicts are essentially unordered

  

例如,我如何能够选择一个月的前24天,然后选择最后一天

如果我对我的回答非常坚定,我可能会看this, which is an Ordered default dict.。但是,您可以将output的数据类型修改为不符合您需求的dict。例如list并根据日期对其进行排序。

答案 1 :(得分:0)

只需减去彼此的2个日期。这将导致timedelta实例。 请参阅datetime.timedelta: https://docs.python.org/2/library/datetime.html#timedelta-objects

from datetime import datetime
delta = datetime.today() - datetime(year=2015, month=01, day=01)
#Actual printed out values may change depending o when you execute this :-)
print delta.days, delta.seconds, delta.microseconds #prints 49 50817 381000 
print delta.total_seconds() #prints 4284417.381 which is 49*24*3600 + 50817 + 381000/1000000

将此与行切片和zip相结合以获得解决方案。一个示例解决方案是:

timestamps = [1176239419.0, 1176334733.0, 1176445137.0, 1177619954.0, 1177620812.0, 1177621082.0, 1177838576.0, 1178349385.0, 1178401697.0, 1178437886.0, 1178926650.0, 1178982127.0, 1179130340.0, 1179263733.0, 1179264930.0, 1179574273.0, 1179671730.0, 1180549056.0, 1180763342.0, 1181386289.0, 1181990860.0, 1182979573.0, 1183326862.0]
timestamps_as_dates = [datetime.fromtimestamp(int(i)) for i in timestamps]
# Make couples of each timestamp with the next one
# timestamps_as_dates[:-1] -> all your timestamps but the last one
# timestamps_as_dates[1:]  -> all your timestamps but the first one
# zip them together so that first and second are one couple, then second and third, ...
intervals = zip(timestamps_as_dates[:-1],timestamps_as_dates[1:])
interval_timedeltas = [(interval[1]-interval[0]).total_seconds() for interval in intervals]
# result = [95314.0, 110404.0, 1174817.0, 858.0, 270.0, 217494.0, 510809.0, 52312.0, 36189.0, 488764.0, 55477.0, 148213.0, 133393.0, 1197.0, 309343.0, 97457.0, 877326.0, 214286.0, 622947.0, 604571.0, 988713.0, 347289.0]

这也适用于从日期添加或减去某个时段:

from datetime import datetime, timedelta
tomorrow = datetime.today() + timedelta(days=1)

我没有一个简单的解决方案来增加或减少数月或数年。

答案 2 :(得分:0)

如果列表按照您的情况排序,那么您可以使用itertools.groupby()将时间戳分组为几天:

#!/usr/bin/env python
from datetime import date, timedelta
from itertools import groupby

epoch = date(1970, 1, 1)

result = {}
assert timestamps == sorted(timestamps)
for day, group in groupby(timestamps, key=lambda ts: ts // 86400):
    # store the interval + day/month in a dictionary.
    same_day = list(group)
    assert max(same_day) == same_day[-1] and min(same_day) == same_day[0]
    result[epoch + timedelta(day)] = same_day[0], same_day[-1] 
print(result)

输出

{datetime.date(2007, 4, 10): (1176239419.0, 1176239419.0),
 datetime.date(2007, 4, 11): (1176334733.0, 1176334733.0),
 datetime.date(2007, 4, 13): (1176445137.0, 1176445137.0),
 datetime.date(2007, 4, 26): (1177619954.0, 1177621082.0),
 datetime.date(2007, 4, 29): (1177838576.0, 1177838576.0),
 datetime.date(2007, 5, 5): (1178349385.0, 1178401697.0),
 datetime.date(2007, 5, 6): (1178437886.0, 1178437886.0),
 datetime.date(2007, 5, 11): (1178926650.0, 1178926650.0),
 datetime.date(2007, 5, 12): (1178982127.0, 1178982127.0),
 datetime.date(2007, 5, 14): (1179130340.0, 1179130340.0),
 datetime.date(2007, 5, 15): (1179263733.0, 1179264930.0),
 datetime.date(2007, 5, 19): (1179574273.0, 1179574273.0),
 datetime.date(2007, 5, 20): (1179671730.0, 1179671730.0),
 datetime.date(2007, 5, 30): (1180549056.0, 1180549056.0),
 datetime.date(2007, 6, 2): (1180763342.0, 1180763342.0),
 datetime.date(2007, 6, 9): (1181386289.0, 1181386289.0),
 datetime.date(2007, 6, 16): (1181990860.0, 1181990860.0),
 datetime.date(2007, 6, 27): (1182979573.0, 1182979573.0),
 datetime.date(2007, 7, 1): (1183326862.0, 1183326862.0)}

如果当天只有一个时间戳,则重复两次。

  

如何测试结果中的最后一个(例如)5个条目是否具有比之前的14更大的间隔?

entries = sorted(result.items())
intervals = [(end - start) for _, (start, end) in entries]
print(max(intervals[-5:]) > max(intervals[-5-14:-5]))
# -> False