python - 使用标准库在运行时创建 ND 数组

标签 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]]

但是它似乎不允许在语言的语法中使用它。有人知道我如何使用标准库执行此操作吗?

最佳答案

[[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]]

关于python - 使用标准库在运行时创建 ND 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55941642/

相关文章:

python - 我如何使用迭代来提高效率?

python - 基于两个不同的数组创建一个二维数组

python - 如何为 `transitions` 状态机定义触发器枚举?

java - 我应该使用什么数组复制方法?

JavaScript 如果变量具有数组中某个项目的indexOf

ios - executeFetchRequest 抛出 fatal error : NSArray element failed to match the Swift Array Element type

python - 对 Groovy 脚本调用的 Python 脚本中的异常进行编码

python - 如何使用 python 获取 HTML 元素中的所有链接?

python - 使用额外功能处理错误

python - 如何在 Python 中表示两个特定日期时间之间的时间段?