python - 将 csv 读入 Python 时跳过相同的值

标签 python numpy

我试图从列表中的下一项中减去列表中的前一项,但我认为我的类型阻止我这样做。列表中每一项的类型都是 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/

相关文章:

python - 为什么定义在 NumPy 文档字符串部分的冒号前有一个空格?

python - 将 3D 列表转换为 numpy 数组

python - Pandas 数据框单元格中的嵌套列表,如何提取?

python - 使用 write 函数编写格式良好的列表

python - 使用python中的beautifulsoup从具有更多文本内容的网页中提取数据

python - 为数百万行优化 python 循环

python - 如何切片和扩展二维 numpy 数组?

python - 在 Pyglet 中使用 numpy 数组和 ctypes 进行 OpenGL 调用

python - 不使用迭代推理连续数据点

python - flask 。使用gevent : [TypeError: 'module' object is not callable]的Socket-io