回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我在numpy array中看到了一个非常奇怪的行为,当我在一个简单的操作中混合了int32和int8数组时,int32数组元素<code>ct[4,0]</code>在得到<code>+= dleng[4]*4</code>的结果时似乎变成了8位:</p>
<pre><code>import numpy as np
In[3]: ct = np.zeros((6,1), np.int32)
In[4]: ct
Out[4]:
array([[0],
[0],
[0],
[0],
[0],
[0]], dtype=int32)
In[5]: dleng = np.zeros((6, 1), np.int8)
In[6]: dleng[0] = 2
dleng[1] = 3
dleng[2] = 4
dleng[3] = 7
dleng[4] = 3
dleng[5] = 5
In[7]: dleng
Out[7]:
array([[2],
[3],
[4],
[7],
[3],
[5]], dtype=int8)
In[8]: ct[4] = 117
In[9]: ct
Out[9]:
array([[ 0],
[ 0],
[ 0],
[ 0],
[117],
[ 0]], dtype=int32)
In[10]: ct[4,0] += dleng[4]*4
In[11]: ct
Out[11]:
array([[ 0],
[ 0],
[ 0],
[ 0],
[-127],
[ 0]], dtype=int32)}
</code></pre>
<p>有人知道为什么会这样吗?在</p>