Python boolean arrays confusion in jupyter IDE

Viewed 50

I am new to boolean arrays and find these statements confusing

import numpy as np

a = np.arange(5)

the output of array a is : array([0, 1, 2, 3, 4]) But when i write down this

b = a[True, True, False, False, False]

and print the array b using

print(b)

the output is :

[]

As far as I understand I want to transfer some elements from array a to array b, but why is b empty? What is happening in this code?

1 Answers

Try nested brackets:

a = np.arange(5)
b = a[[True, True, False, False, False]]
print(b)

Output:

[0 1]
Related