我试图从列表中的下一项中减去列表中的前一项,但我认为我的类型阻止我这样做。列表中每一项的类型都是 int。如果我有一个整数列表,例如
1 2 3 4 5 6 7
如何从 2 中减去 1,从 3 中减去 2,从 4 中减去 3,等等,并在每次操作后打印这个值?
我的列表是 torcount,这是我从 numpy 操作中获得的,这是我尝试的代码:
TorCount=len(np.unique(TorNum))
for i in range(TorCount):
TorCount=TorCount[i]-TorCount[i-1]
print TorCount
谢谢
最佳答案
使用np.diff
:
示例:
>>> xs = np.array([1, 2, 3, 4])
>>> np.diff(xs, n=1)
array([1, 1, 1])
numpy.diff(a, n=1, axis=-1)
Calculate the n-th order discrete difference along given axis.
The first order difference is given by out[n] = a[n+1] - a[n] along the given axis, higher order differences are calculated by using diff recursively.
关于python - 将 csv 读入 Python 时跳过相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30532147/