简体   繁体   English

索引 [::-1] 以同时反转所有 2D 数组行和所有 3D 和 4D 数组列和行 Python

[英]Indexing [::-1] to Reverse ALL 2D Array Rows and ALL 3D and 4D Array Columns and Rows Simultaneously Python

How do you get indexing [::-1] to reverse ALL 2D array rows and ALL 3D and 4D array columns and rows simultaneously?您如何获得索引 [::-1] 以同时反转所有二维数组行和所有 3D 和 4D 数组列和行? I can only get indexing [::-1] to reverse 2D array columns.我只能索引 [::-1] 来反转二维数组列。 Python Python

import numpy as np

randomArray = np.round(10*np.random.rand(5,4))
sortedArray = np.sort(randomArray,axis=1)
reversedArray = sortedArray[::-1]
# reversedArray = np.flip(sortedArray,axis=1)

print('Random Array:')
print(randomArray,'\n')
print('Sorted Array:')
print(sortedArray,'\n')
print('Reversed Array:')
print(reversedArray)

You can reverse a dimensions of a numpy array depending on where you place the ::-1 .您可以根据放置::-1的位置反转numpy数组的尺寸。

Lets take a 3D array.让我们采用 3D 阵列。 For reversing the first dimension:对于反转第一个维度:

reversedArray = sortedArray[::-1,:,:]

For reversing the second dimension:对于反转第二维:

reversedArray = sortedArray[:,::-1,:]

For reversing the third dimension:对于反转第三维:

reversedArray = sortedArray[:,:,::-1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM