簡體   English   中英

TypeError:“ NoneType”對象不支持項目分配

[英]TypeError: 'NoneType' object does not support item assignment

我正在嘗試使用以下代碼根據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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM