gpt4 book ai didi

python - 类型错误 : 'NoneType' object does not support item assignment

转载 作者:太空宇宙 更新时间:2023-11-03 11:31:13 25 4
gpt4 key购买 nike

我正在尝试使用以下代码根据 NumPy 数组特定索引处的值进行一些数学计算

X = np.arange(9).reshape(3,3)
temp = X.copy().fill(5.446361E-01)
ind = np.where(X < 4.0)
temp[ind] = 0.5*X[ind]**2 - 1.0
ind = np.where(X >= 4.0 and X < 9.0)
temp[ind] = (5.699327E-1*(X[ind]-1)**4)/(X[ind]**4)
print temp

但我收到以下错误

Traceback (most recent call last):
File "test.py", line 7, in <module>
temp[ind] = 0.5*X[ind]**2 - 1.0
TypeError: 'NoneType' object does not support item assignment

你能帮我解决这个问题吗?谢谢

最佳答案

fill什么都不返回。

>>> import numpy as np
>>> X = np.arange(9).reshape(3,3)
>>> temp = X.copy()
>>> return_value_of_fill = temp.fill(5.446361E-01)
>>> return_value_of_fill is None
True

替换以下行:

temp = X.copy().fill(5.446361E-01)

与:

temp = X.copy()
temp.fill(5.446361E-01)

关于python - 类型错误 : 'NoneType' object does not support item assignment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19605191/

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