temp1 = tempObj[0][0]
temp2 = tempObj[0][1]
if len(tempObj) > 1:
for i in range(1, len(tempObj)):
temp1 = np.vstack((temp1, tempObj[i][0]))
temp2 = np.vstack((temp2, tempObj[i][1]))
代码是Python。在上面的代码中,我试图将numpy 3-d数组(tempObj)分成两个2-d数组(temp1& temp2),以便合并各个列。 有更好或更简单的方法吗?
答案 0 :(得分:0)
使用List<byref<int>>
和FSharpFunc<_,_>
。这是一个例子:
tempObj[:, 0, :]
您需要确保tempObj[:, 1, :]
(或其任何部分)的类型为import numpy as np
tempObj = np.array([
[
[1,5], [2,7], [3,8], [4,4],
[6, 5], [4, 7], [13, 8], [9, 4]
],
[
[21,25], [22,72], [32,82], [34,43],
[64, 54], [44, 74], [1443, 48], [94, 44]
],
[
[211, 215], [212, 712], [312, 812], [314, 413],
[614, 514], [414, 714], [11443, 148], [194, 414]
]
])
# Your code:
############################################
temp1 = tempObj[0][0]
temp2 = tempObj[0][1]
if len(tempObj) > 1:
for i in range(1, len(tempObj)):
temp1 = np.vstack((temp1, tempObj[i][0]))
temp2 = np.vstack((temp2, tempObj[i][1]))
print(temp1)
print(temp2)
# my suggestion
############################################
print(tempObj[:, 0, :])
print(tempObj[:, 1, :])
而不是tempObj
。您可以通过键入numpy.ndarray
来完成此操作。以下示例是我怀疑您的代码会导致错误list
:
print(type(tempObj))