简体   繁体   中英

Set consecutive equal numbers in an array equal to zero

I have an array like

a = np.array( [ 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1] )

and am looking for a way to set consecutive equal elements to zero:

a_desired =  np.array( [ 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1] )

I've had a pretty unsuccessful time of it so far, I've tried something as simple as

for i in range(len(a)-1):
    if a[i+1] == a[i]:
        a[i+1] = 0 

with output [1 0 1 0 0 0 0 1 0 1 0 0 1] , as well as adding more conditions, like

for i in range(len(a)-1):
    if a[i+1] == a[i]:
        a[i+1] = 0 
    if a[i+1] != a[i] and a[i] == 0 and a[i+1] != a[i]:
        a[i+1] = 0 

which has output [1 0 0 0 0 0 0 0 0 0 0 0 0] , but I can't seem to be able to successfully capture all the conditions required to make this work.

Some help would be appreciated!

I would do it following way:

import numpy as np
a = np.array([1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1])
a[1:][a[:-1]==a[1:]] = 0
print(a)

output:

[1 0 0 0 0 0 0 1 0 0 0 0 1]

I compare a without last element with a without first element, thus I do pair-wise comparison between what might be called previous element and current element, which result in array of True s and False s which is 1 shorther then a , then I use it as mask to set 0 where is True . Note that I only modify part of a after first element, as first will never change.

Try numpy xor

np.insert((np.logical_xor(a[:-1], a[1:]) * a[1:]), 0, a[0])
array([1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1])

Try:

import numpy as np

a = np.array([1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1])
a_desired = np.zeros(a.shape)

for i in range(len(a)-1, -1, -1):
    if a[i] == a[i-1] and i != 0:
        a_desired[i] = 0
    else:
        a_desired[i] = a[i]

print(a_desired)

Output:

[1. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 1.]

How about:

value_detected = 0
for i in range(len(a)):
    if value_detected:
        if a[i] == value_detected:
            a[i] = 0
        else:
            value_detected = a[i]
    else:
        if a[i]:
            value_detected = a[i]

print(a)

For original input, the output:

[1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]

Further test, if input is:

a = [ 1, 1, 2, 2, 3, 3, 3, 1, 1, 1, 1, 0, 1]

Then output is:

[1, 0, 2, 0, 3, 0, 0, 1, 0, 0, 0, 0, 1]

Create a list with one element which would be the first element of input list.

Now, just iterate through your list starting from 2nd element and check if it is equal to the previous value.

If yes append 0 else, append the value.

input_arr = [ 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1]
output_arr = [input_arr[0]]
for i in range(1, len(input_arr)):
  if input_arr[i]==input_arr[i-1]:
    output_arr.append(0)
  else:
    output_arr.append(input_arr[i])


print (output_arr)

From me, first i make copy of original array and then make new desired array like this:

new_a = a.copy()
for i in range(1, len(a)):        
    if a[i] == a[i-1]: new_a[i] = 0
print(new_a)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM