将值分配给numpy中的多个片 [英] Assign value to multiple slices in numpy
本文介绍了将值分配给numpy中的多个片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Matlab中,您可以将值分配给同一列表的多个切片:
In Matlab, you can assign a value to multiple slices of the same list:
>> 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
如何用numpy数组在Python中执行此操作?
How can you do this in Python with a numpy array?
>>> 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
推荐答案
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
这篇关于将值分配给numpy中的多个片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文