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!
Aucun commentaire:
Enregistrer un commentaire