不规​​则的切割/复印在numpy的阵列 [英] irregular slicing/copying in numpy array

查看:124
本文介绍了不规​​则的切割/复印在numpy的阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假如我有10个元素,例如数组 A = np.arange(10)。如果我想创建一个与第1,第3,第5,第7,第9,原数组的第十届元素,即 B = np.array([0,2,4,6,8另一个数组, 9]),我怎么能有效地做到这一点?

感谢


解决方案

  A [0,2,4,6,8,9]

指数 A 与列表或阵列重新presenting所需的指数。 (不 1,3,5,7,9,10 ,因为索引从0开始),这是一个有点混乱,该指数和值是一样的在这里,所以有一个不同的例子:

 >>>一个= np.array([5,4,6,3,7,2,8,1,9,0])
>>>一个[0,2,4,6,8,9]
阵列([5,6,7,8,9,0])

请注意,这将创建一个副本,而不是一个视图。 此外,请注意,这可能不会推广到多个轴你期望的方式。

Suppose I have an array with 10 elements, e.g. a=np.arange(10). If I want to create another array with the 1st, 3rd, 5th, 7th, 9th, 10th elements of the original array, i.e. b=np.array([0,2,4,6,8,9]), how can I do it efficiently?

thanks

解决方案

a[[0, 2, 4, 6, 8, 9]]

Index a with a list or array representing the desired indices. (Not 1, 3, 5, 7, 9, 10, because indexing starts from 0.) It's a bit confusing that the indices and the values are the same here, so have a different example:

>>> a = np.array([5, 4, 6, 3, 7, 2, 8, 1, 9, 0])
>>> a[[0, 2, 4, 6, 8, 9]]
array([5, 6, 7, 8, 9, 0])

Note that this creates a copy, not a view. Also, note that this might not generalize to multiple axes the way you expect.

这篇关于不规​​则的切割/复印在numpy的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆