计算差异

Calculate the difference

我有数据文件:file
我需要进行 X 差异(第 1 列),但仅当第 3 列为 1 时。

import numpy as np

x,jump= np.loadtxt("data.svc",delimiter=' ',skiprows=1, usecols=(0,3),unpack=True)

resultX = list()

i=0
while (i<len(jump)):
    if jump[i] == 1:
        while(jump[i] == 1):
            i+=1
            temp = i
            resultX.append((abs(x[temp]-x[temp-1])))
    i+=1

print(resultX)

我的结果是:5,7,4,12,8,6,9,5,4,11错了

我需要:5,7,4,8,6,5,4 correct results

我认为这会起作用:

>>> diff = np.diff(x)
>>> diff
array([ 5,  7,  4, 12,  4, 15,  8,  6,  9,  5,  5,  4,  5,  4, 11])
>>> 
>>> flag = np.diff(jump) + jump[1:]
>>> flag
array([ 1,  1,  1, -1,  0,  2,  1,  1, -1,  0,  0,  2,  1,  1, -1])
>>> 
>>> diff[flag == 1]
array([5, 7, 4, 8, 6, 5, 4])