具有2維數組,我想克隆n *次它的每個項目。這與在兩個維度上放大圖像n次完全相同。 這裏是我的代碼:數組操作:錯誤:'int'對象不支持項目分配
def elargir(a,n) :
imag=[a.shape[0]*n,a.shape[1]*n] # Array with the wanted shape
for i in range(a.shape[0]):
for j in range(a.shape[1]*n): # loops on lines and columns of a
imag[i][j]=a[i//5][j//n]
return imag
我創建一個數組
a=np.array([[1,2],[3,4]])
,並在其應用功能
elargir (a,5)
,這裏是thte錯誤
Traceback (most recent call last):
File "<ipython-input-14-508f439a1888>", line 1, in <module>
elargir (a,5)
File "<ipython-input-12-b2382eb5b301>", line 5, in elargir
imag[i][j]=a[i//5][j//n]
TypeError: 'int' object does not support item assignment
感謝你的幫助
[類型錯誤: 'INT' 對象不支持項目分配]的可能的複製(http://stackoverflow.com/questions/14805306/typeerror-int-object-does-not-support-item-assignment ) –
這個'imag = [a.shape [0] * n,a.shape [1] * n]'創建了一個包含兩個元素的列表:a.shape [0] * n'和'a.shape [1 ] * N'。 – khelwood