开满天机
使用无for循环的一种非常快速的单行解决方案是这样的:# initializationqDCTReversed = np.arange(4*3*3).reshape((4,3,3)) # calculationqDCTReversed = qDCTReversed.reshape((2,2,3,3)).transpose((0,2,1,3)).reshape((6,6))或对于(400,8,8)数组:qDCTReversed.reshape((20,20,8,8)).transpose((0,2,1,3)).reshape((160,160))速度对比:Mstaino 的回答:0.393 毫秒yatu 的回答:0.138 毫秒这个答案:0.016 毫秒
慕田峪9158850
您要求的重塑可以通过以下方式完成:x = np.arange(36).reshape((4,3,3))np.vstack(np.hstack(x[2*i:2+2*i]) for i in range(x.shape[0]//2))>>array([[ 0, 1, 2, 9, 10, 11], [ 3, 4, 5, 12, 13, 14], [ 6, 7, 8, 15, 16, 17], [18, 19, 20, 27, 28, 29], [21, 22, 23, 30, 31, 32], [24, 25, 26, 33, 34, 35]])
婷婷同学_
您可以通过循环遍历列表来做到这一点:a = [[[ 0, 1, 2], [ 9,10,11]], [[ 3, 4, 5], [12,13,14]], [[ 6, 7, 8], [15,16,17]], [[18,19,20], [27,28,29]], [[21,22,23], [30,31,32]], [[24,25,26], [33,34,35]]]b = [[i for j in k for i in j ] for k in a]print(b)输出:[ 0, 1, 2, 9, 10, 11][ 3, 4, 5, 12, 13, 14][ 6, 7, 8, 15, 16, 17][18, 19, 20, 27, 28, 29][21, 22, 23, 30, 31, 32][24, 25, 26, 33, 34, 35]