<p><code>dleng[4]*4</code>是一个<em>数组</em>:</p>
<pre><code>In [94]: dleng[4]
Out[94]: array([3], dtype=int8)
In [95]: dleng[4]*4
Out[95]: array([12], dtype=int8)
</code></pre>
<p>而<code>ct[4, 0]</code>是类型为<code>np.int32</code>的<em>标量</em>:</p>
^{pr2}$
<p>正如@WallyBeaver指出的,<code>ct[4,0] += dleng[4]*4</code>就像<code>ct[4,0] = ct[4,0] + dleng[4]*4</code>。最后一个表达式是一个<em>标量</em>加上一个<em>数组</em>。在这种情况下,数据类型是由<em>数组</em>决定的,因此它最终是<code>np.int8</code>。在<a href="http://docs.scipy.org/doc/numpy/reference/ufuncs.html" rel="nofollow">numpy docs</a>中有一条关于此的注释:</p>
<blockquote>
<p>Mixed scalar-array operations use a different set of casting rules
that ensure that a scalar cannot “upcast” an array unless the scalar
is of a fundamentally different kind of data (i.e., under a different
hierarchy in the data-type hierarchy) than the array. This rule
enables you to use scalar constants in your code (which, as Python
types, are interpreted accordingly in ufuncs) without worrying about
whether the precision of the scalar constant will cause upcasting on
your large (small precision) array.</p>
</blockquote>
<p>修复方法是将就地加法写成</p>
<pre><code>ct[4,0] += dleng[4,0]*4
</code></pre>