0

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.

1

1 Answer 1

-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

Sign up to request clarification or add additional context in comments.

5 Comments

Or just aaa.reshape(arr.shape[:-1])
Or even simpler aaa[:,:,0]
Thanks for the ideas everybody. I dont understand how this is interpreted as a change in dimensions though. I start with a 2D array and want to end with a 2D array. Regardless aaa[:,0] does what I want and gives an array of numbers
You actually start with a 3D array: aaa.shape returns (3,20,1)
You can also mark the answer as accepted for future users and better reference

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.