擅长:python、mysql、java
<p>原始内存没有区别。但从逻辑上讲,一个是由两个值组成的一维数组,另一个是二维数组(其中一个维度恰好是大小为1)。在</p>
<p>逻辑区别对于<code>numpy</code>很重要;当您尝试添加它们时,它希望创建一个新的2x2数组,其中最上面的一行是<code>(2, 1)</code>数组顶部“row”与{<cd3>}数组中每个值的和。但是,如果使用<code>+=</code>来执行此操作,则表明您希望能够在适当的位置修改<code>(2,)</code>数组,如果不调整大小,这是不可能的(而<code>numpy</code>不会这样做)。如果您从以下位置更改代码:</p>
<pre><code>arr1 += arr2
</code></pre>
<p>收件人:</p>
^{pr2}$
<p>它将愉快地创建一个新的<code>(2, 2)</code>数组。或者,如果目标是2x1数组应该像平面1D数组一样,那么您可以<code>flatten</code>它:</p>
<pre><code>alreadyflatarray += twodarray.flatten()
</code></pre>