I have an array A
. I want to identify all indices with element 1 and print as a list. But I am getting an error. I present the expected output.
import numpy as np
A=np.array([[1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0]])
A1=np.where(A[0]==1)
A1.tolist()
print(A1)
The error is
in <module>
A1.tolist()
AttributeError: 'tuple' object has no attribute 'tolist'
The expected output is
[[0, 2, 3, 5]]
>Solution :
A1 = [i for i,x in enumerate(A[0]) if x == 1]
or
A1=list(np.where(A[0]==1)[0])
You have an array inside an array hence A[0]