Assign value to multiple slices in numpy

有些话、适合烂在心里 提交于 2019-12-05 02:21:37
a = np.arange(10)
a[[range(3)+range(6,9)]] = 10
#or a[[0,1,2,6,7,8]] = 10 

print a

that should work I think ... I dont know that its quite what you want though

You might also consider using np.r_:

http://docs.scipy.org/doc/numpy/reference/generated/numpy.r_.html

ii = np.r_[0:3,7:10]
a[ii] = 10

In [11]: a
Out[11]: array([ 10, 10, 10,  3,  4,  5,  6, 10, 10,  10])

From http://docs.scipy.org/doc/numpy/user/basics.indexing.html (Section "Index Arrays"). Note that indices for desired slices should be contained within 'np.array()'.

>>> x = np.arange(10,1,-1)
>>> x
array([10,  9,  8,  7,  6,  5,  4,  3,  2])

>>> x[np.array([3, 3, 1, 8])]
array([7, 7, 9, 2])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!