Python 使用标准库在运行时创建ND阵列

Python 使用标准库在运行时创建ND阵列,python,arrays,python-3.x,numpy,Python,Arrays,Python 3.x,Numpy,我有三个数组 a = [2] b = [2,3,6] c = [1] 我想合并它们,这样我就得到了一个大小为len(a)*len(b)的数组,其中包含这两者的所有排列。(C将始终包含一个值) 我以为这样的事情会奏效 newArr = [for i in range len(a)*len(b) [for x in a][for y in b][for z in c]] print(newArr) [[2,2,1],[2,3,1],[2,6,1]] 然而,它似乎不允许在语言的语法中使用它。有人

我有三个数组

a = [2]
b = [2,3,6]
c = [1]
我想合并它们,这样我就得到了一个大小为
len(a)*len(b)
的数组,其中包含这两者的所有排列。(C将始终包含一个值)

我以为这样的事情会奏效

newArr = [for i in range len(a)*len(b) [for x in a][for y in b][for z in c]]
print(newArr)

[[2,2,1],[2,3,1],[2,6,1]]
然而,它似乎不允许在语言的语法中使用它。有人知道我是如何使用标准库的吗

[[x, y, z] for x in a for y in b for z in c]
例如:

>>> [[x, y, z] for x in [2] for y in [2,3,6] for z in [1]]
[[2, 2, 1], [2, 3, 1], [2, 6, 1]]
使用

import itertools

a = [2]
b = [2,3,6]
c = [1]

p = itertools.product(a, b, c)

print(list(p))

[(2, 2, 1), (2, 3, 1), (2, 6, 1)]