我这样创建了两个3D阵列
import numpy as np
np.random.seed(1)
prices = np.random.randint(10, 20, size = (2, 2, 2)
#prices -> [[[19 10] [[19, 19]
# [14 17]], [19, 16]]]
clients = []
for i in range (3):
if i == 0:
clients.append(np.random.randint(1, 10, size = (2,2)))
elif i == 1:
clients.append(np.random.randint(11, 20, size = (2, 2)))
elif i == 2:
clients.append(np.random.randint(21, 30, size = (2, 2)))
z = np.array(clients)
# z -> [[[ 8 3] [[14, 19] [[21 26]
# [ 2 6]] [16, 15]] [21 25]]]
然后我就可以连接两个数组了
concatenate = []
for i in prices:
for j in z:
concatenate.append(np.concatenate((i,j), axis = 1))
prices2 = np.array(concatenate)
# prices2 ->[[[19 10 8 3] [[19 10 14 19] [[19 10 21 26] [[19 19 8 3] [[19 19 14 19] [[19 19 21 26]
# [14 17 2 6]] [14 17 16 15]] [14 17 21 25]] [19 16 2 6]] [19 16 16 15]] [19 16 21 25]]]
我想要的是使用每个2D数组第三列中的信息以升序对3D数组进行排序,例如:
# new = [[[19 10 8 3] [[19 19 8 3] [[19 10 14 19] [[19 19 14 19] [[19 10 21 26] [[19 19 21 26]
# [14 17 2 6]] [19 16 2 6]] [14 17 16 15]] [19 16 16 15]] [14 17 21 25]] [19 16 21 25]]]
#Here 8 < 14 < 21
# 2 < 16 < 21
我知道numpy有一个名为np.sort
的函数,但是我看到的许多示例都使用2D数组而不是3D数组,而且我真的不知道该怎么做。