[英]How to undo an array inside of a list in Python?
I know there are several topics about flattening lists and arrays.我知道有几个关于扁平化列表和数组的主题。 But I could not find an answer to my question in specific.
但我找不到具体问题的答案。 I am trying to get all possible combinations of a matrix with a vector as follows:
我正在尝试获取矩阵与向量的所有可能组合,如下所示:
import numpy as np
from itertools import product
Var1 = np.array([1,2,3])
Var2 = np.array([10,20,30])
Var3 = np.array([100,200,300])
Var4 = np.array([500,1000,1500])
First3Var = [Var1,Var2,Var3]
Combinations = list(product(First3Var, Var4))
This works fine, "Combinations" is almost what I want.这很好用,“组合”几乎是我想要的。 However, I now get an array inside a list.
但是,我现在在列表中得到一个数组。 I want it to be an array with 4 columns, not with 2. Does anyone know how I undo this nested array?
我希望它是一个有 4 列的数组,而不是 2 列。有谁知道我如何撤消这个嵌套数组? I hope my question is clear.
我希望我的问题很清楚。 Thanks!
谢谢!
You can do [np.append(i,j) for (i,j) in Combinations]
and use it as a list or as an np.array()
.您可以执行
[np.append(i,j) for (i,j) in Combinations]
并将其用作列表或np.array()
。
import numpy as np
from itertools import product
Var1 = np.array([1,2,3])
Var2 = np.array([10,20,30])
Var3 = np.array([100,200,300])
Var4 = np.array([500,1000,1500])
First3Var = [Var1,Var2,Var3]
Combinations = product(First3Var, Var4)
Combinations = np.array([np.append(i,j) for (i,j) in Combinations])
Now Combinations
is a 4-column matrix.现在
Combinations
是一个 4 列矩阵。
[[ 1 2 3 500]
[ 1 2 3 1000]
[ 1 2 3 1500]
[ 10 20 30 500]
[ 10 20 30 1000]
[ 10 20 30 1500]
[ 100 200 300 500]
[ 100 200 300 1000]
[ 100 200 300 1500]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.