1

I have a 2D numpy array of single element lists:

aaa = np.array(
[[ [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], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ],
 [ [0], [4], [4], [4], [4], [4], [4], [4], [4], [4], [4], [4], [4], [4], [4], [4], [4], [4], [4], [4] ] ]
)

How can I turn the inner list into an int so I would have:

nnnn = np.array(
    [[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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
     [0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 ]]
)

It sounds simple but anything I have tried I still end up with a list.

I tried sum() as a technique for summing the values in a list, but only ended up summing the whole lot.

Kins
  • 547
  • 1
  • 5
  • 22
jc508
  • 91
  • 5
  • I think the simplest solution is in the second dupe https://stackoverflow.com/questions/37152031/numpy-remove-a-dimension-from-np-array - `aaa[:,:,0]` – Tomerikoo Jul 07 '22 at 08:38

1 Answers1

-1

You can do:

nnnn = np.array([l.flatten() for l in aaa])

You can also use reshape:

nnnn = aaa.reshape(-1, aaa.shape[1])

Or even simpler:

nnnn = aaa[:,:,0]

It is useful to note that the last solution returns a view, not a copy. Meaning that changes you apply to the new array will also be reflected in the old array

Kins
  • 547
  • 1
  • 5
  • 22