George
George

Reputation: 5691

transform value as array

I have this code:

import numpy as np

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

try:
    # something like that
    [ x =  np.array([x]) for x in a if x == 0]
except ValueError:
    pass

I want to replace every zero value as an array, so my result would be:

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

Upvotes: 0

Views: 49

Answers (1)

nir0s
nir0s

Reputation: 1181

Your list comprehension is not valid.

Use:

x = [np.array([x]) for x in a if x == 0]

instead.

Do note that the logic here doesn't provide the relevant answer but rather:

x = array([1, 2])

in the end.

For what you're expecting:

Use:

np.array([0]) if x is 0 else x for x in a

Upvotes: 2

Related Questions