0

I want to slice an array without knowing its dimensions. Indexes(start and end are given in list format. How to do this? Thank you.

Slice 1d array

import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])
# idx: [2, 5)
print(a[2:5])
# [3, 4, 5]

Slice 2d array

import numpy as np
a = np.array([
[1, 2, 3],
[2, 3, 2],
[4, 5, 2]
])

start = [0, 1]
end = [1, 2]

print(a[start[0]:end[0], start[1]:end[1]])

Slice N-d array?

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

start = [0, 0, 1]
end = [1, 1, 2]

result = a[start[0]:end[0], start[1]:end[1], start[2]:end[2]] # key point: how to adapt this?
print(result)
2
  • You need to get len(a.shape) to find out the number of dimensions and then you can loop through all of the dimensions to find out the number of values in the dimension. Commented Dec 17, 2020 at 5:20
  • This is a thought. Let me try. Commented Dec 17, 2020 at 5:21

1 Answer 1

1

If you have a list of start and end indexes, you can construct a tuple of slice objects and do the following to slice an array without knowing its dimensions:

a[tuple(slice(*indexes) for indexes in zip(start, end))]

Or:

a[tuple(slice(st, en) for st, en in zip(start, end))]

Or:

from itertools import starmap

a[tuple(starmap(slice, zip(start, end)))]

In action:

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

start = [0, 0, 1]
end = [1, 1, 2]

result1 = a[start[0]:end[0], start[1]:end[1], start[2]:end[2]] # key point: how to adapt this?

result2 = a[tuple(slice(*indexes) for indexes in zip(start, end))] 

assert np.all(result2 == result2) 
Sign up to request clarification or add additional context in comments.

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.