DSblizzard
DSblizzard

Reputation: 4149

Numpy: how to make parameterized slice?

I have this:

slice = ar[starts[0] : ends[0], starts[1] : ends[1], starts[2] : ends[2]]

How to make something like this:

slice = ar[starts[i] : ends[i] for i in range(3)]

Upvotes: 0

Views: 72

Answers (1)

Daniel F
Daniel F

Reputation: 14399

First, don't use slice as a variable name, as it is a builtin.

Second, what you are looking for uses that builtin:

ar[tuple(slice(s, e) for s, e in zip(starts, ends))]

Upvotes: 1

Related Questions