python - 如何映射具有相同索引的 2 个 numpy 数组

标签 python numpy

我正在尝试将 2 个 numpy 数组映射为 [x, y],类似于 zip 对列表和元组所做的。

我有 2 个 numpy 数组,如下所示:

arr1 = [1, 2, 3, 4]
arr2 = [5, 6, 7, 8]

我正在寻找一个 输出为 np.array([[[1, 5], [2, 6], [3, 7], [4, 8]]])

我试过了,但它映射了每个值,而不是使用相同的索引。我可以在此处添加更多 if 条件,但有没有其他方法可以在不添加更多 if 条件的情况下这样做。

res = [arr1, arr2] for a1 in arr1 for a2 in arr2]

最佳答案

您正在寻找np.dstack

Stack arrays in sequence depth wise (along third axis).

np.dstack([arr1, arr2])

array([[[1, 5],
        [2, 6],
        [3, 7],
        [4, 8]]])

关于python - 如何映射具有相同索引的 2 个 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50955960/

相关文章:

python - 将附件添加到电子邮件消息会引发 TypeError : set_text_content() got an unexpected keyword argument 'maintype'

python 循环遍历用户条目

python - python 中的社区检测算法

python - 如何在 python 中将复数四舍五入到小数点后两位?

python - 迭代数据框

python - pip如何确定python包版本

python - Qt 布局不反射(reflect)组框大小变化

python - 创建一个范围内的随机整数序列,并且它们之间的距离最小

python - reshape numpy 数组

python - 多个 numpy place 调用会产生奇怪的结果