【问题标题】:How to bind elements of the same index in numpy array in python如何在python的numpy数组中绑定相同索引的元素
【发布时间】:2022-01-08 20:32:07
【问题描述】:

我正在尝试创建一个函数,将两个不同子数组的相同索引的元素绑定到一个 numpy 数组中。 例如,如果输入是input = [[1,2,3],[4,5,6]],我想要它output =[(1,4),(2,5),(3,6)] 或类似的东西。子数组的数量可以变化(输入为 *input) 我要么在最后将新绑定的元素加在一起。例如,[1+4、2+5、3+6]。 我知道一种解决方法,但我想知道是否有一些内置函数可以做到这一点。

我尝试过的 -numpy.add() 函数需要两个 numpy 数组,所以这不起作用

  • for x,y in zip (*input[i] for i in len(lists)) 给出参数不足的错误

【问题讨论】:

标签: python arrays python-3.x numpy


【解决方案1】:

如果你最终对元素的总和感兴趣,我会这样做:

import numpy as np
input = np.array([[1,2,3],[4,5,6]])
np.sum(input, axis=0)

输出 = 数组([5, 7, 9])

如果你确实想在某个时候拥有中间数组,你只需要一个转置:

input.T

输出 = 数组([[1, 4], [2, 5], [3, 6]])

【讨论】:

    猜你喜欢
    • 2015-08-15
    • 1970-01-01
    • 2016-05-24
    • 2023-01-07
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 2017-12-01
    相关资源
    最近更新 更多