2017-04-19 3 views
-1

2次元配列を持つので、アイテムのそれぞれにn *回クローンします。 これは、両方のディメンションでイメージをn倍に拡大するのとまったく同じです。 thteエラーの配列操作:エラー: 'int'オブジェクトがアイテム割り当てをサポートしていません

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 

おかげで、私は配列

a=np.array([[1,2],[3,4]]) 

を作成し、それに

elargir (a,5) 

を関数を適用

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 

とここにある: はここに私のコードですあなたの助け

+0

[例外TypeError:「int型のオブジェクトは、アイテムの割り当てをサポートしていません]の可能な重複(http://stackoverflow.com/questions/14805306/typeerror-int-object-does-not-support-item-assignment ) –

+0

この 'imag = [a.shape [0] * n、a.shape [1] * n]'は、 'a.shape [0] * n'と' a.shape [1] ] * nである。 – khelwood

答えて

0

imagは1Dアレイである。 for i in range(a.shape[0]):では、最初にその配列の最初の項目であるintにアクセスし、さらにjでインデックスを作成しようとします。あなたの例の配列は、最初のコードブロックで得られたものと一致しません。

import numpy as np 

a = np.arange(9).reshape(3, 3) 
n = 3 

imag=[a.shape[0]*n,a.shape[1]*n] 
print(imag[0]) # The `i` index in your nested for loop 
+0

ありがとうございました。 np.zerosでnp.arrayを置き換え、imag配列の権利形状を取得しました –

+0

imag = np.zeros([a.shape [0] * n、a.shape [1] * n)) –

+0

:)あなたがこの回答があなたの問題を解決したことが分かったら、[正しいとマークする](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)を考慮して、問題が解決したことを知ってください。 – roganjosh

関連する問題