我有一个全部转换为整数的时间戳列表。如何将它们分成2秒间隔的单独列表?
d = [1499256400, 1499256400, 1499256400, 1499256400, 1499256400, 1499256400, 1499256400, 1499256400, 1499256400, 1499256401, 1499256401, 1499256401, 1499256401, 1499256402, 1499256402, 1499256403, 1499256403, 1499256404, 1499256404, 1499256405, 1499256405, 1499256405, 1499256405, 1499256406, 1499256406, 1499256407, 1499256407, 1499256408, 1499256408, 1499256408, 1499256408, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409]
我想将它们分成2秒的间隔,这样我就可以打印出每个间隔的结果。
预期输出:
[1499256400, 1499256400, 1499256400, 1499256400, 1499256400, 1499256400, 1499256400, 1499256400, 1499256400, 1499256401, 1499256401, 1499256401, 1499256401, 1499256402, 1499256402]
[1499256403, 1499256403, 1499256404, 1499256404, 1499256405, 1499256405, 1499256405, 1499256405]
[1499256406, 1499256406, 1499256407, 1499256407, 1499256408, 1499256408, 1499256408, 1499256408]
[1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409]
感谢。
答案 0 :(得分:3)
您可以使用简单的for循环对最大差异为2的项目进行分组:
lst = [[]]
start = d[0]
for x in d:
if x-start <= 2:
lst[-1].append(x)
else:
lst.append([x])
start = x
如果元素未排序,您可以在分组前调用sorted
进行预测。
对于代码重用(导入及所有这些),您可以创建一个grouper类,然后使用itertools.groupby
进行分组:
from itertools import groupby
class Grouper(object):
def __init__(self, start=None, diff=2):
self.diff = diff
self.start = start
self._count = 0
def __call__(self, x):
if x-self.start > self.diff:
self._count += 1
self.start = x
return self._count
然后分组现在简化为:
lst = [list(g) for _, g in groupby(d, Grouper(start=d[0]))]
[[1499256400, 1499256400, 1499256400, 1499256400, 1499256400, 1499256400, 1499256400, 1499256400, 1499256400, 1499256401, 1499256401, 1499256401, 1499256401, 1499256402, 1499256402],
[1499256403, 1499256403, 1499256404, 1499256404, 1499256405, 1499256405, 1499256405, 1499256405],
[1499256406, 1499256406, 1499256407, 1499256407, 1499256408, 1499256408, 1499256408, 1499256408],
[1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409]]
答案 1 :(得分:1)
您可以尝试这一点 - 确定d
的最小值和最大值的范围,然后检查每个元素是否在2秒(x, x+1, x+2)
内的时间间隔内:
for x in range(min(d), max(d)+1, 3):
print ([item for item in d if item in (x, x+1, x+2) ])
返回:
[1499256400, 1499256400, 1499256400, 1499256400, 1499256400, 1499256400, 1499256400, 1499256400, 1499256400, 1499256401, 1499256401, 1499256401, 1499256401, 1499256402, 1499256402]
[1499256403, 1499256403, 1499256404, 1499256404, 1499256405, 1499256405, 1499256405, 1499256405]
[1499256406, 1499256406, 1499256407, 1499256407, 1499256408, 1499256408, 1499256408, 1499256408]
[1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409, 1499256409]