我有一个 numpy 对象数组,其中包含多个索引号列表:
>>> idxLsts = np.array([[1], [0, 2]], dtype=object)
我定义了一个向量化函数来向每个列表附加一个值:
>>> idx = 99
>>> f = np.vectorize(lambda idxLst: idxLst.append(idx))
我调用该函数。我不关心返回值,只关心副作用。
>>> f(idxLsts)
array([None, None], dtype=object)
索引 99 被两次添加到第一个列表中。为什么?我很困惑。
>>> idxLsts
array([[1, 99, 99], [0, 2, 99]], dtype=object)
对于 idxLsts 的其他值,不会发生这种情况:
>>> idxLsts = np.array([[1, 2], [0, 2, 4]], dtype=object)
>>> f(idxLsts)
array([None, None], dtype=object)
>>> idxLsts
array([[1, 2, 99], [0, 2, 4, 99]], dtype=object)
我怀疑它与文档有关:“定义一个向量化函数,它需要一个nested对象序列或 numpy 数组作为输入,并返回 numpy 数组作为输出。向量化函数像 python map 函数一样在输入数组的连续元组上评估 pyfunc,但它使用 numpy 的广播规则。”