Note that view holds the same data as the original array! Which can result in unexpected results. But you seem to want only the diagonal, you could do that with stride tricks as well to make sure you do not copy data if you want (next versions will create a view with diagonal, old ones always a copy):diagonal = np.diagonal(view, axis1=0, axis2=1)# unfortunatly now the first slice is diagonal[...,0], so just roll it to the start:diagonal = np.rollaxis(diagonal, -1)现在,diagonal是您在for循环中创建的数组(在较新的版本上,如果您不想要视图,请添加.copy()).Now diagonal is the array you created in your for loop (on newer versions add a .copy() if you do not want a view).由于slices数组是2D而不是3D数组(因为您追加了),因此此处缺少整形: Since the slices array is 2D and not 3D because you append, a reshape was missing here:slices = diagonal.reshape(-1,2)如果您有这么小的数组,这可能不会更快,但是它的常数(期望在diagonal调用中复制数据)与数组大小有关.This might not be faster if you have such small arrays, but its constant (expect for the copying of the data in diagonal call) with the array size. 这篇关于numpy:将多个切片组装到新数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-20 18:42