移动NumPy数组中的所有索引 [英] Shift all indices in NumPy array
本文介绍了移动NumPy数组中的所有索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的numpy数组:
I have a numpy array like this:
x=np.array([0,1,2,3,4])
并要创建一个数组,其中索引0中的值位于索引1中,索引1中的索引位于索引2中,依此类推.
and want to create an array where the value in index 0 is in index 1, index 1 is in index 2, etc.
我想要的输出是:
y=np.array([0,0,1,2,3]).
我猜想有一个简单的方法可以做到这一点,而无需遍历整个数组.如何以numPythonic的方式做到这一点?
I'm guessing there's an easy way to do this without iterating through the full array. How can I do this in a numPythonic way?
推荐答案
您可以使用
y = numpy.roll(x, 1)
y[0] = 0
或
y = numpy.r_[0, x[:-1]]
这篇关于移动NumPy数组中的所有索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文