0

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])

2
  • a = [np.array([0]) if x is 0 else x for x in a] Commented Mar 7, 2017 at 8:34
  • Hah @falsetru. Just noticed you got it first in the comment. Commented Mar 7, 2017 at 8:37

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.