vendredi 9 juillet 2021

Update values correctly using if ternary

I am trying to update values of an array givinga certain condition, using this code:

a=[np.inf, 2, 3]

for i in range(0,10):
    a[0] = 100 if a[0] == np.inf else a.append(1)
    print(a)

The firs iteration works perfectly fine, and changes the infinity values per 100. However, in the subsequent iterations it adds the number 1, but change the first values of the array by None.

[100, 2, 3] 
[None, 2, 3, 1] 
[None, 2, 3, 1, 1] 
...
[None, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1]

Why is this happening?

Aucun commentaire:

Enregistrer un commentaire