gpt4 book ai didi

python - 更改 numpy 数组列表中的值

转载 作者:行者123 更新时间:2023-12-01 06:59:16 24 4
gpt4 key购买 nike

我有一个 numpy 数组列表

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

如何将第二个数组设置为负数?如果我写 nplist[0][1] 我得到

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

那为什么我做不到nplist[0][1] = -nplist[0][1]然后我得到

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

最佳答案

因为tuple是不可变的。预先将其更改为list:

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

输出:

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

关于python - 更改 numpy 数组列表中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58706465/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com