1

I have a list like this:

x = [array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.]),
     array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])]

and then I declare another list:

y = [2, 8]

How can I replace an element in the first list if the value equals to 2 (or in index of y[0]) with 0? And do the same thing on the second list, which I should replace the value 8 with 0?

So the desired output should be like this:

x = [array([ 0.,  1.,  0.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.]),
     array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  0.,  9., 10.])]
0

5 Answers 5

3

Here's a vectorized solution. When both x and y are NumPy arrays

Make your x and y NumPy arrays. Then use this approach.

x[x==y[:,None]]=0

Complete example:

x=np.array([[ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.],
            [ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  0.,  9., 10.]])
y=np.array([2,8])

x[x==y[:,None]]=0

#array([[ 0.,  1.,  0.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.],
#       [ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  0.,  9., 10.]])

Timeit analysis:

In [50]: timeit x[x==y[:,None]]=0 #My solution
2.2 µs ± 34.6 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [53]: timeit [np.where(a==v, 0, a)  for v, a in zip(y, x)] #kederrac's solution
10.6 µs ± 309 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [54]: %%timeit
    ...: x = [np.array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.]),
    ...:      np.array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])]
    ...: y = [2, 8]
    ...: for i in range(len(x)):
    ...:   arr = x[i]
    ...:   arr[arr == y[i]] = 0
    ...:
6.61 µs ± 310 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) #Sam's solution
Sign up to request clarification or add additional context in comments.

Comments

1

The following code will replace all values of the each array in x with the corresponding index of the values in y.

import numpy as np

x = [np.array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.]),
     np.array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])]

y = [2, 8]

for i in range(len(x)):
  arr = x[i]
  arr[arr == y[i]] = 0

For future note, you are using both numpy arrays and python lists, yet you are calling them both lists. Try not to do this as they are different things and it might get confusing when you get into more complicated projects

1 Comment

Nice answer. I added your timeit analysis to my answer.
1

you can use a list comprehension with np.where and the built-in function zip:

x = [np.where(a==v, 0, a)  for v, a in zip(y, x)]
x

output:

[array([ 0.,  1.,  0.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.]),
 array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  0.,  9., 10.])]

to be a bit faster since np.where will return a new array you could filter base on y and change the value without creating a new array:

for a, v in zip(x, y):
    x[x==v] = 0 

3 Comments

Without casting this can be used. Nice answer.+1
I timeit Sam's answer it faster than list comprehension may be the culprit is np.where. I'll time your answer using == instead of np.where.
@Ch3steR sam is doing in-place replacements, np.where is returning a new array
0

Solution:

x = [[ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.],
     [ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.]]
     
y = [2, 8]

for i, array in enumerate(x):
    for j, number in enumerate(array):
        if number == y[i]:  # this might fail for floats
            array[j] = 0.0
    print(array)

Output:

[0.0, 1.0, 0.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 0.0, 9.0, 10.0]

Notes:

Note that comparing floats can become tricky. If you know you will only compare integers, best to cast them as such at least, or use some package/module to properly compare them (either by comparing their difference to a very small delta, or using decimal.Decimal field instead, for example)

Comments

0

Assuming the lists are of the same size, which you can check like this:

if len(x) == len(y):
    print("Lists are the same size")

You can loop through and check for the values:

for i in range(len(x)):        # For each array in x
    for j in range(len(x[i])): # For each item in the array
        if x[i][j] == y[i]:    # If the item is equal to the value you are searching
            x[i][j] = 0

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.