TypeError:“NoneType”对象不支持项分配

2024-06-06 17:41:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用下面的代码根据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

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


Tags: and代码numpynp数组wherefilltemp
1条回答
网友
1楼 · 发布于 2024-06-06 17:41:26

^{}不返回任何内容。

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

相关问题 更多 >