我有三个列表,每个列表包含2个元素。如何获得一个(8x3)数组,其中包含三个列表中元素的所有可能组合(带替换)?
示例:
vec1 = [4, 6]
vec2 = [2, 4]
vec3 = [1, 5]
output = [[4, 2, 1], [4, 2, 5], [4, 4, 1], [4, 4, 5], [6, 2, 1], [6, 4, 5], [6, 2, 5], [6, 4, 1]]
这是我的代码(简体):
import scipy.stats as st
percentiles = [0.01, 0.99]
draws1 = st.norm.ppf(percentiles, 0, 1)
draws2 = st.norm.ppf(percentiles, 0, 1)
draws3 = st.norm.ppf(percentiles, 0, 1)
您可以像这样使用numpy.meshgrid():
np.array(np.meshgrid([4, 6], [2, 4], [1, 5])).T.reshape(-1,3)
将导致:
array([[4, 2, 1],
[4, 4, 1],
[6, 2, 1],
[6, 4, 1],
[4, 2, 5],
[4, 4, 5],
[6, 2, 5],
[6, 4, 5]])
您可以使用简单的for
循环:
import numpy as np
vec1 = [4, 6]
vec2 = [2, 4]
vec3 = [1, 5]
output = np.zeros([8,3])
counter = 0
for i in vec1:
for j in vec2:
for k in vec3:
temp = [i,j,k]
output[counter,:] = [i,j,k]
counter += 1