-1

I have an array A. I want to identify all locations with element 1 and convert it to a list as shown in the expected output. But I am getting an error.

import numpy as np

A=np.array([0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

B=np.where(A==1)

B=B.tolist()
print(B)

The error is

in <module>
    B=B.tolist()

AttributeError: 'tuple' object has no attribute 'tolist'

The expected output is

[1, 2, 5, 7, 10, 11]
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 2
    Have you tried printing `B` before you call `tolist`? – Plagon Feb 14 '23 at 10:39
  • Welcome to Stack Overflow. Please think carefully about the logic. "I want to identify all locations with element 1 and convert it to a list as shown in the expected output." - **what** do you want to "convert"? It is not the original array `A`, but **the locations** that you got back from the `.where` call - right? So. **Is that result an array**? What is it instead? – Karl Knechtel Feb 14 '23 at 10:48
  • Please also read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ and try to *examine* problems before asking. For example, if you get an error message from code like `B=B.tolist()`, then it should be clear that there is a problem with trying to use `.tolist` on `B` - therefore, the next step in reasoning about the problem, should be to **check what `B` is**. When you do this, you will find that you have an ordinary Python `tuple`, not a Numpy array. Then you can see what is in that tuple: **one** element, which is the array that you want. – Karl Knechtel Feb 14 '23 at 10:49

3 Answers3

2

np.where used with only the condition returns a tuple of arrays containing indices; one array for each dimension of the array. According to the docs, this is much like np.nonzero, which is the recommended approach over np.where. So, since your array is one dimensional, np.where will return a tuple with one element, inside of which is the array containing the indices in your expected output. You can resolve your problem by accessing into the tuple like np.where(A == 1)[0].tolist().

However, I recommend using np.flatnonzero instead, which avoids the hassle entirely:

import numpy as np

A = np.array([0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

B = np.flatnonzero(A).tolist()

B:

[1, 2, 5, 7, 10, 11]

PS: when all other elements are 0, you don't have to explicitly compare to 1 ;).

Chrysophylaxs
  • 5,818
  • 3
  • 10
  • 21
1
import numpy as np

A = np.array([0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

indices = np.where(A == 1)[0]
B = indices.tolist()

print(B)
MUHAMMED IQBAL PA
  • 3,152
  • 2
  • 15
  • 23
1

You should access the first element of this tuple with B[0] :

import numpy as np

A=np.array([0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

B=np.where(A==1)

B = B[0].tolist()
print(B) # [1, 2, 5, 7, 10, 11]
Phoenixo
  • 2,071
  • 1
  • 6
  • 13