在Matlab中,可以为同一列表的多个切片指定一个值:
>> a = 1:10
a =
1 2 3 4 5 6 7 8 9 10
>> a([1:3,7:9]) = 10
a =
10 10 10 4 5 6 10 10 10 10
如何在Python中使用numpy数组实现这一点?
>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a[1:3,7:9] = 10
IndexError: too many indices
Tags:
我想这应该管用。。。我不知道这正是你想要的
从http://docs.scipy.org/doc/numpy/user/basics.indexing.html(节“索引数组”)。请注意,所需切片的索引应包含在“np.array()”中。
您还可以考虑使用
np.r_
:http://docs.scipy.org/doc/numpy/reference/generated/numpy.r_.html
相关问题 更多 >
编程相关推荐