I have the following
U = np.array([1., 0., 0., 1., 0., 0., 1., 0., 1., 0.])
c = iter(range(2, len(U)))
result = [float(next(c)) if x == 1 else x for x in U]
This gives
[2.0, 0.0, 0.0, 3.0, 0.0, 0.0, 4.0, 5.0, 6.0, 0.0
] However I want that when the element on the left is label a number that the one the 'loop' is on becomes the same label.
So this [2.0, 0.0, 0.0, 3.0, 0.0, 0.0, 4.0, 4.0, 4.0, 0.0
]
A for-loop that describes this is the following:
U=np.array([1., 0., 0., 1., 1., 0., 1., 1., 1.])
l=2
for i in range(len(U[:])):
i+1 #for simplicity I skipped the first one
if U[i]==1:
if U[i-1]!=0:
U[i]=U[i-1]
else:
U[i]=l
l+=1
This returns
array([1., 0., 0., 2., 2., 0., 3., 3., 3.])
Aucun commentaire:
Enregistrer un commentaire