0

I have a numpy array of shape (1,2,3,4,5,6,...) (arbitrary length) and I would like to select the first entry in the first n columns, i.e.,

def select_first_n_0(arr, n):
    if n == 1:
        return arr[0]
    elif n == 2:
        return arr[0][0]
    elif n == 2:
        return arr[0][0][0]
    # ...

Is there a more comprehensive expression for this?

1
  • 1
    Create an indexing tuple, e.g. idx=(0,0,0). Commented Feb 20, 2021 at 16:32

2 Answers 2

1

You can use recursion

def select_first_n_0(arr, n):
   if n == 1:
      return arr[0]
   return select_first_n_0(arr[0], n-1)
Sign up to request clarification or add additional context in comments.

Comments

0

After @hjpaul:

def select_first_n_0(arr, n):
    return arr[tuple([0] * n)]

Comments

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.