时间轴操纵

时间:2018-03-20 07:52:51

标签: python time axis

我有几秒钟的时间向量:

time = [0,1,2,4,5,7,0,1,2,4,6,7,8,0,1,2,3]

def get_elapsed_time(time):

for i in range(0,len(time)-1):

    if time[i+1] > time[i]:
        elapsed_time.append(time[i])
    else:
        tr = time[i]
        t1 = tr + 1e-3 # add 1 millisecond to keep both values..
        elapsed_time.append(tr)
        elapsed_time.append(t1)
        while time[i+1] > time[i]:
              t2 = time[i] + tr
              elapsed_time.append(t2)                                              

elapsed_time = []

get_elapsed_time(time)

我正在尝试创建一个函数(上面),它将调整时间轴,以便在计数器重置为零时对值进行求和。即我想要实现的时间向量输出是:

elapsed_time = [0,1,2,4,5,7,7.001,8.001,9.001,11.001,13.001,14.001,15.001,15.002,16.002,17.002,18,002]

这似乎是一个相对简单的任务,使用循环来修复这种类型的时间向量,但对于我的生活,我无法弄明白。我的代码给了我错误的值,如下所示:

elapsed time = [0, 1, 2, 4, 5, 7, 7.001, 0, 1, 2, 4, 6, 7, 8, 8.001, 0, 1, 2]

对此的任何帮助将不胜感激...... !!感谢。

约旦。

2 个答案:

答案 0 :(得分:0)

据我了解你的代码,添加毫秒并不是必需的,而你的while循环没有做任何事情。我认为你应该改变你的方法,并且只是跟踪检测到即将发生的重置的时间:

def get_elapsed_time(time):
    elapsed_time = []
    last_time_before_reset = 0
    for i in range(0,len(time)-1):
        current_time = last_time_before_reset + time[i]
        elapsed_time.append(current_time)
        if time[i+1] <= time[i]:
            # upcoming time reset
            last_time_before_reset = current_time
    return elapsed_time

答案 1 :(得分:0)

应该有很好的time = [0,1,2,4,5,7,0,1,2,4,6,7,8,0,1,2,3] #multiply by 1000 to avoid float math problems and work just with integers time = [1000 * i for i in time] newtime = time[:1] #counter to keep track of resets s = 0 for i, t in enumerate(time[1:]): #adjust counter if t == 0: s += time[i] + 1 #append list newtime.append(t + s) #reverse multiplication by 1000 newtime = [i / 1000 for i in newtime] print(newtime) #output [0.0, 1.0, 2.0, 4.0, 5.0, 7.0, 7.001, 8.001, 9.001, 11.001, 13.001, 14.001, 15.001, 15.002, 16.002, 17.002, 18.002] 解决方案,但这是一个简单的Python循环。

AppDelegate