1

I have a numpy array and would like to subset the first two arrays of each element in an ndarray.

Here is an example array:

import numpy as np

a1 = np.array([[ 1,  2,  3],
               [ 4,  5,  6]])

a2 = np.array([[ 7,  8,  9],
               [10, 11, 12],
               [13, 14, 15],
               [16, 17, 18]])

a3 = np.array([[19, 20, 21],
               [22, 23, 24],
               [25, 26, 27]])

A = np.array([a1, a2, a3])

print("A =\n", A)

Which prints:

A =
   [array([[ 1,  2,  3],
           [ 4,  5,  6]])
    array([[ 7,  8,  9],
           [10, 11, 12],
           [13, 14, 15],
           [16, 17, 18]])
    array([[19, 20, 21],
           [22, 23, 24],
           [25, 26, 27]])]

The desired result is as follows:

A =
   [array([[ 1,  2,  3],
           [ 4,  5,  6]])
    array([[ 7,  8,  9],
           [10, 11, 12]])
    array([[19, 20, 21],
           [22, 23, 24]])]

To print the equivalent object, you could do

print(np.array([a1[0:2], a2[0:2], a3[0:2]]))

But I want to directly get what is desired using A.

What is the correct way of doing this in numpy?

Edit: I would like to subset the array without looping. Alternative ways of structuring the arrays so that they can be directly indexed are okay too. Any numpy function to avoid looping is fair game.

4
  • This doesn't make sense to me. You have essentially 3 ndarrays of different sizes. You then put them into a single ndarray, which is then of size 3. How do you expect to slice those without indexing them individually? Commented Jul 20, 2019 at 21:58
  • Is there an alternative way to structure the arrays? Ideally, they would be subsetable without looping. Commented Jul 20, 2019 at 22:13
  • 1
    Operating on A (object dtype array) is essentially the same as operating on the list [a1,a2,a3]. Both contain pointers to the 3 arrays. In fact looping on the list is faster. There isn't a 'no-loop' means of indexing the subarrays of A. Now if the subarrays all had the same shape, A would be (3,3,3) shaped, and you could A[:, 0:2, :] slice on the middle dimension. Commented Jul 20, 2019 at 22:35
  • hpaulj's response is the closest to what I am looking for. But it seems to be an expensive cost to pad them all -- making the second dimension the largest length of a1, a2, or a3 just to enable indexing up to the length of the smallest. Commented Jul 21, 2019 at 1:36

1 Answer 1

2

a = [i[0:2] for i in A]

This will work!

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

3 Comments

List comprehension are essentially a for loop and the OP say he doesn't want loops...
He edited it to say without a for loop after my answer. I understand that list comprehension is a loop. But even if there was a numpy method for this, it would still loop.
Let's be picky about this. It isn't question of loops or no loops. It's a question of whether we do the looping at interpreted Python level, or within compiled numpy code. With object dtype most operations on the objects are at Python speed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.