我有一個函數可以處理具有多個項目的數組,但如果數組只包含一個項目,則會失敗。讓我們看看這個例子TypeError/Array索引; 'int'對象不支持項目分配
import numpy as np
def checker(a):
a[a>5] = np.nan
a = np.arange(10)
a = checker(a)
作品,但
a = 1
a = checker(a) # fails
,並給出
Traceback (most recent call last):
a[a>5] = np.nan
TypeError: 'int' object does not support item assignment
我想處理它像MATLAB,而不是像這個版本檢查的()比上面的版本多出4倍的線。
def checker(a):
try:
a[a>5] = np.nan
except TypeError:
if a>5: a = np.nan
你是什麼試圖在這裏做? – Eric
我試圖計算一些東西,只要滿足一些條件 – Lukas