简体   繁体   English

如何通过具有单例尺寸的1d数组在python中索引没有单例尺寸的1d数组?

[英]How to index 1d array without singleton dimension in python by 1d array with singleton dimension?

what is the procedure to index variations of 1d arrays in python? 在python中索引一维数组的变体的过程是什么?

For example, consider the following example: 例如,考虑以下示例:

a = np.ones(100)
b = np.ones(100, 1)
a[b > 0]
IndexError                                Traceback (most recent call last)
<ipython-input-14-05e3ddce24c9> in <module>()
----> 1 a[b == 1]

IndexError: too many indices for array

How would I do this indexing, without creating a new array? 我如何在不创建新数组的情况下进行索引?

Only a is a 1D array. 一维数组只有a Singleton dimensions are still dimensions. 单件尺寸仍然是尺寸。

Remove the second dimension from b : b删除第二维:

a[b[:, 0] > 0]

b[:, 0] takes a view of b 's data, not a copy. b[:, 0]查看b的数据,而不是副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM