Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.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中的新数组大小和值覆盖数组大小_Python_Arrays_Numpy - Fatal编程技术网

用python中的新数组大小和值覆盖数组大小

用python中的新数组大小和值覆盖数组大小,python,arrays,numpy,Python,Arrays,Numpy,我有以下输出的python代码: # Somecode block before ##################### print np.shape(data2['sac']['startx'][0][0]) print np.shape(startx_buff) # New Completed Assignment data2['sac']['startx'][0][0][0] = startx_buff #**shape of data2['sac']['startx'][0][0]

我有以下输出的python代码:

# Somecode block before
#####################
print np.shape(data2['sac']['startx'][0][0])
print np.shape(startx_buff)
# New Completed Assignment
data2['sac']['startx'][0][0][0] = startx_buff

#**shape of data2['sac']['startx'][0][0] = (2, 119, 20)**

#**shape of startx_buff = (2,120,20)**

#***Error: data2['sac']['startx'][0][0][0] = startx_buff
ValueError:无法将输入数组从形状2120,20广播到形状119,20***

我基本上想覆盖data2['sac']['startx'][0][0]条目,以获得2120,20维以及startx_buff的值。原因:在后处理之后,我注意到缺少一个条目值。在MATLAB中,这是没有问题的,因为它更灵活,我可以通过为变量指定一个新值和数组/单元形状来重新定义变量。但我在python中尝试的其他解决方法不起作用的是:

del data2['sac']['startx'][0][0]
data2['sac']['startx'][0][0] = np.empty((2,120,20))
#Error: won't let me delete all the values in the array
我不太确定解决方案是什么,甚至在“在线查找内容”下,我尝试查找覆盖/重载数组大小。除了找到这个问题的答案外,这个问题的正确术语是什么?

解决方案和输入错误,如Willem标记的:


索引被[0]关闭,名称更简单我们不需要所有的字典索引,您要做的是:

In [212]: X = np.zeros((2,119,20),int)

In [214]: y = np.ones((2,120,20),int)
In [215]: X[0,:,:] = y
...    
ValueError: could not broadcast input array from shape (2,120,20) into shape (119,20)
MATLAB确实允许某种自动尺寸增长,但不知何故,我怀疑它是否如此灵活。那将是危险的。我在MATLAB工作的时候从来没有依赖过这种行为。在任何情况下,numpy都期望一个精确的匹配——在广播规则中,它增加了很多MATLAB没有或至少没有的功能

重点是X[0,:,:]我添加了:为了读者的清晰度,是119,20插槽。2120,20块不可能放在那里

这确实可以减少LHS,使其适合:

X[0,:,:] = y[0, :119, :]
我们也可以用一个连接来生长X

In [219]: X1 = np.concatenate((X, np.zeros((2,1,20),int)), axis=1)
In [220]: X1.shape
Out[220]: (2, 120, 20)
In [221]: X1[0,:,:] = y[0,:,:]
“调整大小”也可以工作,但“连接”可以更好地控制填充值:

In [222]: np.resize(X, (2,120,20)).shape
Out[222]: (2, 120, 20)
np.Empty2120,20是具有该形状的完整数组。它只有在值未指定的情况下才是空的。numpy没有列表的等价物,x[1:3]=[]有一个np.delete函数

data2['sac']['startx'][0][0]表达式有点不清楚。在Python中,这看起来像是两级字典访问,然后是两级列表索引

请记住,numpy数组大致相当于MATLAB矩阵,python列表更像MATLAB单元格。像单元格这样的列表可以容纳任何内容,包括数组

In [227]: alist[0]=np.arange(3)
In [228]: alist
Out[228]: [array([0, 1, 2]), [2, 3], None]
In [227]: alist[0]=np.arange(3)
In [228]: alist
Out[228]: [array([0, 1, 2]), [2, 3], None]