1

I have a list of numpy arrays

nplist =  [(array([[2.50000000e+01, 4.09600003e+03, 4.33334732e+00],
           [2.50000000e+01, 1.36533336e+03, 7.00000381e+00],
           [1.50000000e+01, 1.22880000e+04, 1.00002670e+00],
           [1.50000000e+01, 2.59413333e+04, 3.33362579e-01],
           [2.50000000e+01, 2.86734757e+04, 4.16426142e+00],
           [2.00000000e+01, 5.99947347e+03, 2.27296132e+00]]), array([[-0.65173667],
           [-0.65174361],
           [-0.63682517],
           [-0.65175054],
           [-0.64986406],
           [-0.64614659]]))]

How can I set the second array negative? If I write nplist[0][1] I get

array([[-0.65173667],
       [-0.65174361],
       [-0.63682517],
       [-0.65175054],
       [-0.64986406],
       [-0.64614659]])

So why I can't do nplist[0][1] = -nplist[0][1] Then I get

Traceback (most recent call last):
  File "/snap/pycharm-professional/167/helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<input>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
2
  • It's already negative Commented Nov 5, 2019 at 7:32
  • You mean positive? Commented Nov 5, 2019 at 7:33

1 Answer 1

1

Its because tuple is an immutable. Change it into list beforehand:

nplist[0] = list(nplist[0])
nplist[0][1] = -nplist[0][1] 
nplist[0][1]

Output:

array([[0.65173667],
       [0.65174361],
       [0.63682517],
       [0.65175054],
       [0.64986406],
       [0.64614659]])
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.