import numpy as np
a=[[1,2,3,4],
[5,6,7,8],
[9,10,11,12]]
print("列表a如下:")
print(a)
print("增加一维,新维度的下标为0")
c=np.stack(a,axis=0)
print(c)
print("增加一维,新维度的下标为1")
c=np.stack(a,axis=1)
print(c)
输出:
列表a如下:
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
增加一维,新维度的下标为0
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
增加一维,新维度的下标为1
[[ 1 5 9]
[ 2 6 10]
[ 3 7 11]
[ 4 8 12]]
可以从上面看出 axis为0和1的时候则是分别从行和列进行重新堆叠成相应的数据结构
a = [[1,2,3]]
b = [[4,5,6]]
c = np.hstack((a,b))
print(c)
[[1 2 3 4 5 6]]
a = [[1,2,3],["a","b","c"]]
b = [["ff","dd","gg"],[7,8,9]]
c = np.hstack((a,b))
print(c)
[['1' '2' '3' 'ff' 'dd' 'gg']
['a' 'b' 'c' '7' '8' '9']]
可以看到分别将相同维度的数据堆叠在了一起