Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 数组操作:错误:';int';对象不支持项分配_Python_Arrays - Fatal编程技术网

Python 数组操作:错误:';int';对象不支持项分配

Python 数组操作:错误:';int';对象不支持项分配,python,arrays,Python,Arrays,有一个二维数组,我想克隆它的每个项的n*倍。 这就像在两个维度上将图像放大n倍。 这是我的密码: 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

有一个二维数组,我想克隆它的每个项的n*倍。 这就像在两个维度上将图像放大n倍。 这是我的密码:

    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)
这是错误

    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
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
elargir(a,5)
文件“”,第5行,elargir格式
imag[i][j]=a[i//5][j//n]
TypeError:“int”对象不支持项分配

感谢您的帮助

imag是一个1D阵列。在
中,对于范围内的i(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

这个
imag=[a.shape[0]*n,a.shape[1]*n]
的可能重复项创建了一个包含两个元素的列表:
a.shape[0]*n
a.shape[1]*n
。非常感谢。用NP.0替换NP-数组,得到IMAG ARAYIMAG=NP.0的权限形状([ApHeal[0 ] *N,AsPosi[ 1 ] *N]),这就更有意义了:如果你发现这个答案解决了你的问题,那么请考虑其他人知道这个问题已经解决了。