相邻元素之间的差异 [英] Numpy difference between neighboring elements

查看:61
本文介绍了相邻元素之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有计算纯python中相邻元素之间差异的算法:

I have algorithm of calculation of the difference between neighboring elements in pure python:

    a = range(1000000) #it's numpy array in my case
    prev = a[0]
    b = [0, ]
    for i in a[1:]:
        b.append(i - prev)
        prev = i

有什么方法可以用Numpy重写此功能吗?

Is there any way to rewrite this functions with Numpy?

推荐答案

diff 方法:

a = range(5) # python list of numpy array
np.diff(a)

返回

array([1, 1, 1, 1])

这篇关于相邻元素之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆