1

I have two arrays in Python.

array1 looks like this:

[[ 59.13425446  85.62345123]
 [ 59.4981575   87.67746735]
 [ 57.4234575   83.34333335]]

array2 looks like this:

[[ 255  255]
 [ 1.0   255]
 [ 255   1.0]]

I want to update array1. For every value in array2 that equals 1.0, I want to update array1 with a value of 0.0. In the end it should look like this:

[[ 59.13425446  85.62345123]
 [ 0.0   87.67746735]
 [ 57.4234575   0.0]]

How can I do this in Python?

1 Answer 1

5

Something like:

array1[array2 == 1] = 0
Sign up to request clarification or add additional context in comments.

3 Comments

This looks muuuuch cleaner than mine but didnt seem to work. The output was [0, [59.4981575, 87.67746735], [57.4234575, 83.34333335]]. Did you get something else?
@Jas are you using (numpy) arrays or lists?
@Jas This method doesn't work for regular Python lists. array2 == 1 returns just False if array2 is a list. With numpy arrays, the expression evaluates to an array of bools, which are then used as indexes for the assignment.

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.