简体   繁体   English

python方式访问numpy数组的移位版本?

[英]Pythonic way to access the shifted version of numpy array?

What is a pythonic way to access the shifted, either right or left, of a numpy array? 什么是访问numpy数组右移或左移的numpy A clear example: 一个清晰的例子:

a = np.array([1.0, 2.0, 3.0, 4.0])

Is there away to access: 是否可以访问:

a_shifted_1_left = np.array([2.0, 3.0, 4.0, 1.0])

from the numpy library? numpy库?

You are looking for np.roll - 您正在寻找np.roll

np.roll(a,-1) # shifted left
np.roll(a,1) # shifted right

Sample run - 样品运行-

In [28]: a
Out[28]: array([ 1.,  2.,  3.,  4.])

In [29]: np.roll(a,-1) # shifted left
Out[29]: array([ 2.,  3.,  4.,  1.])

In [30]: np.roll(a,1) # shifted right
Out[30]: array([ 4.,  1.,  2.,  3.])

If you want more shifts, just go np.roll(a,-2) and np.roll(a,2) and so on. 如果您想要更多班次,只需转到np.roll(a,-2)np.roll(a,2) ,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM