通常,如果试图在numpy中分配超过数组末尾的元素,则不存在的元素将被忽略。
>>> x = np.zeros(5)
>>> x[3:6] = np.arange(5)[2:5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not broadcast input array from shape (3) into shape (2)
但是,如果仅分配了一个元素,则完全超过数组末尾的相同操作“成功”:
>>> x[5:] = np.arange(5)[4:]
>>> x[5:] = np.arange(5)[4:100]
>>> x[5:] = np.arange(5)[3:]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not broadcast input array from shape (2) into shape (0)