1

Recently I learned some ways not to use for loops in numpy, since they make code inefficient and numpy enables you to work without for loops and more with lambda expressions.

So now I wonder if there is a possibility to do the following without any for loop, by creating some arrays before calculating:

Let's say I have an array with tuples:

slice_indexes = np.array([(0,9),(1,10),(2,11),(3,12),...])

Then we have an array we want to slice by those tuples:

data_array[i:j]

Is this achievable without a for loop? If yes? How?

9
  • Before I even review the code: "...numpy enables you to work without for loops and more with lambda expressions". Where does lambda come into this? You shouldn't be using lambdas with numpy unless absolutely necessary Commented Jun 7, 2020 at 18:13
  • Do all of your tuples contain the same size slice (in this case 9 elements)? Also what is the shape of your input array? Commented Jun 7, 2020 at 18:13
  • Would all slices have the same lengths? Like, in the given sample it's seems to be 9. Commented Jun 7, 2020 at 18:19
  • If the resulting slices differ in size, then some sort of Python loop is necessary. Doubly so if the slices overlap. You can avoid loops only if the slicing pattern is regular, and can produce a 2d array. Commented Jun 7, 2020 at 18:28
  • yes. All slices would have the same length. Commented Jun 7, 2020 at 18:28

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.