[英]How to copy numpy array values at certain indices
I have a numpy array and I want to copy parts of the array at certain indices to a different array. 我有一个numpy数组,我想将某些索引处的数组部分复制到另一个数组。
arr = np.arange(10)
np.random.shuffle(arr)
print arr
[0 3 4 2 5 6 8 7 9 1]
I want to copy the value at the indices 我想在索引处复制值
copy_indices = [3, 7, 8]
Is there any good way to do this? 有什么好办法吗?
How about using this approach? 使用这种方法怎么样?
In [16]: arr
Out[16]: array([2, 9, 5, 6, 1, 4, 7, 8, 3, 0])
In [17]: copy_indices
Out[17]: [3, 7, 8]
In [18]: sliced_arr = np.copy(arr[copy_indices, ])
# alternatively
# In [18]: sliced_arr = arr[copy_indices, ]
In [19]: sliced_arr
Out[19]: array([6, 8, 3])
PS: Advanced indexing (as here) actually returns copy of the array . PS:高级索引(如此处所示)实际上返回数组的副本 。 So, the use of
np.copy()
is optional. 因此,
np.copy()
的使用是可选的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.