简体   繁体   English

Numpy 中的索引

[英]Indexing in Numpy

In the following example, why does the first numpy index gives [1], but the second numpy index gives [ ]?在下面的示例中,为什么第一个 numpy 索引给出 [1],而第二个 numpy 索引给出 []? Thanks in advance for the help!在此先感谢您的帮助!

a = np.array([1,2,3])
print(a[a<2])
>>> [1]
print(a[True, False, False])
>>> []

for the first one, you are selecting index: a[a<2] = a[[True, False, False]] which is different to your second.对于第一个,您正在选择 index: a[a<2] = a[[True, False, False]]这与您的第二个不同。 a[True, False, False]

for your second, you are selecting True, False, False index, which doesn't exists in the array.对于您的第二个,您正在选择True, False, False索引,该索引在数组中不存在。 so nothing returns.所以没有任何回报。

here you told the numpy array a to select the elements which are less than 2 and there is only one element it is the element "1" which has as index 0 in your array.在这里,您告诉 numpy 数组select 的元素小于 2 ,并且只有一个元素是元素“1” ,它在数组中具有索引 0。

look at this illustrative example:看看这个说明性的例子:

print(a[a<1])
>>> []
print(a[a<2])
>>> [1]
print(a[a<3])
>>> [1,2]
print(a[a<4])
>>> [1,2,3]

try尝试

print(a[[True, False, False]])

instead of代替

print(a[True, False, False])

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

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