Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take array of indices, find matching indices in another array and replace values

I have two arrays, one array that contains all indices of two arrays that meet a certain condition I had made previous to this. The other array is an array of booleans. I want to take the array of indices and find the same place in the array of booleans and replace those values.

So for example what I am looking to do is:

myIdxs = [0, 3, 5]
myBools = [1, 0, 0, 1, 1, 1, 0, 1, 0, 1] 

and change myBools to:

myBools = [0, 0, 0, 0, 1, 0, 0, 1, 0, 1]

I've tried:

myBools = [myBools[i] for i in myIdx == 0]

But this does not give me the desired output.

like image 782
lo_rabb Avatar asked Feb 01 '26 23:02

lo_rabb


1 Answers

I hope this works (not sure what you need):

myIdxs = [0, 3, 5]
myBools = [1, 1, 1, 1, 1, 0]

myBools = [myBools[i] if i in myIdxs else 0
        for i in xrange(len(myBools))]

>>> print myBools
[1, 0, 0, 1, 0, 0]
like image 131
th3an0maly Avatar answered Feb 04 '26 12:02

th3an0maly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!