[::-1] 数组中元素反向
觉得有用的话,欢迎一起讨论相互学习~
![](https://img2020.cnblogs.com/blog/949241/202004/949241-20200404123341456-2091227860.png)
![我的微博](https://img2020.cnblogs.com/blog/949241/202004/949241-20200404125636151-1411970006.png)
![我的github](https://img2020.cnblogs.com/blog/949241/202004/949241-20200404125603323-1670542535.jpg)
![我的B站](https://img2020.cnblogs.com/blog/949241/202004/949241-20200404142715197-1867301511.jpg)
一维数组反向[::-1]
import numpy as np
a = np.arange(8)
print ("生成0-19之间的数组",a)
# 一维数组反向
a1 = a[::-1]
print("将a数组反向",a1)
# 生成0-19之间的数组 [0 1 2 3 4 5 6 7]
# 将a数组反向 [7 6 5 4 3 2 1 0]
高维数组[::-1]没有反向效果
# 高维数组则没有反向效果
# 将数组转换为(2,4)
# 将数组转换为(4,2)
a_2_4 = a1.reshape(2, 4)
a_4_2 = a1.reshape(4, 2)
print("将数组转换为(2,4)
",a_2_4)
print("将数组转换为(4,2)
",a_4_2)
a_reshape_2_4 = a1.reshape(2, 4)[::-1]
a_reshape_4_2 = a1.reshape(4, 2)[::-1]
print("将(2,4)数组反向
",a_reshape_2_4)
print("将(4,2)数组反向
",a_reshape_4_2)
"""将数组转换为(2,4)
[[7 6 5 4]
[3 2 1 0]]
将数组转换为(4,2)
[[7 6]
[5 4]
[3 2]
[1 0]]
将(2,4)数组反向
[[3 2 1 0]
[7 6 5 4]]
将(4,2)数组反向
[[1 0]
[3 2]
[5 4]
[7 6]]"""