3

From a 2D numpy array Z, I'd like to remove certain points, at indicies ix, iy.
ix, iy are currently sequential lists of the x-index and corresponding y-index, like so:

ix = range(10)
iy = range(10,20,1)

So I'd like Z[0][10], Z[1][11], etc. put into a new list or array.

What is the best (read: most pythonic) way of doing this? Perhaps there is a better way of finding these indicies so I get an array of [ix,iy], [ix+1,iy+1] etc.?

I have looked at this question. I don't know how to use the list comprehension syntax of the selected answer for 2 dimensions.

2 Answers 2

2

Can't test now, but AFAIK it could be done like Z[ix, iy] in numpy.
Also, you don't have to write range(10,20,1), you can use range(10,20)

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

Comments

0
[Z[y][x] for x, y in zip(ix, iy)]

(What zip(ix, iy) does is: make a list of tuples [(ix[0], iy[0]), (ix[1], iy[1]), ...], i.e. the x and y indices for each element we need.)

1 Comment

This works too, but I had to flip the index to Z[x][y], which depends on how you define the x-axis. Thanks!

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.