【问题标题】:Concatenate numpy arrays inside a function and return it?在函数内连接numpy数组并返回它?
【发布时间】:2013-03-04 16:26:38
【问题描述】:

如何在一个函数内连接两个 numpy 数组并考虑以下程序返回它

#!/usr/bin/env python
import numpy as np

def myfunction(myarray = np.zeros(0)):
    print "myfunction : before = ", myarray    # This line should not be modified
    data = np.loadtxt("test.txt", unpack=True) # This line should not be modified
    myarray = np.concatenate((myarray, data))
    print "myfunction : after = ", myarray     # This line should not be modified
    return                                     # This line should not be modified

myarray = np.array([1, 2, 3])
print "main : before = ", myarray
myfunction(myarray)
print "main : after = ", myarray

这段代码的结果是:

main : before =  [1 2 3]
myfunction : before =  [1 2 3]
myfunction : after =  [ 1.  2.  3.  1.  2.  3.  4.  5.]
main : after =  [1 2 3]

我想要:

main : before =  [1 2 3]
myfunction : before =  [1 2 3]
myfunction : after =  [ 1.  2.  3.  1.  2.  3.  4.  5.]
main : after =  [ 1.  2.  3.  1.  2.  3.  4.  5.]

如何修改提供的程序以获得预期的结果(# This line should not be modified标记的4行应该保持不变)?

【问题讨论】:

  • 文森特,我不相信你所要求的在这里是完全可能的。您可以将列表传递给函数,添加元素,并且将修改传入的原始列表。但是,numpy 数组不能像列表那样增长或缩小。 concatenate 函数创建了一个全新的对象,这就是为什么原始对象 array([1,2,3]) 没有改变的原因。正如 Dave 所说,您必须返回新的数组串联数组对象才能使用它。
  • 我还不相信这是不可能的。但是,即使可以就地修改 myarray,您当然也不希望将其与具有默认值 (myarray = np.zeros(0)) 的它结合起来。
  • 我认为您正在遭受XY problem 的痛苦。你有一个问题,你认为你知道一种可以解决它的方法,所以你问的是如何让你的方法发挥作用——即使它不能很好地适应 Python 的对象模型——而不是问如何解决您的问题。

标签: python arrays numpy pass-by-reference


【解决方案1】:

你应该返回值

像这样修改函数:

def myfunction(myarray = np.zeros(0)):
    print "myfunction : before = ", myarray    # This line should not be modified
    data = np.loadtxt("test.txt", unpack=True) # This line should not be modified
    concatenated = np.concatenate((myarray, data))
    print "myfunction : after = ", myarray     # This line should not be modified
    return  concatenated

然后你会得到这样的结果

result = myfunction(myarray)

【讨论】:

  • 我将此行标记为不可修改,因为当我在单个函数中有 2 个数组要修改时,我该怎么做? (我的代码就是这种情况)
  • 什么意思?您需要连接两个数组对吗? concatenated = np.concatenate((myarray, data)) 然后返回concatenated object
  • 我有 myarray1、myarray2、data1、data2,我需要连接(myarray1 和 data1)和(myarray2 和 data2)。
  • 您可以为每个方法返回多个值,因此您可以执行类似return array1,array2,array3 ...等的操作
【解决方案2】:

您可以执行以下操作,但可能会出错:

def in_place_concatenate(arr1, arr2) :
    n = len(arr1)
    arr1.resize((n + len(arr2),), refcheck=False)
    arr1[n:] = arr2

正如你所料:

>>> a = np.arange(10)
>>> b = np.arange(4)
>>> in_place_concatenate(a, b)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3])

但是:

>>> a = np.arange(10)
>>> b = np.arange(4)
>>> c = a[:5]
>>> c
array([0, 1, 2, 3, 4])
>>> in_place_concatenate(a, b)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3])
>>> c
array([         1, 1731952544,   71064376,          1,   67293736])

如果您尝试修改 c 中的任何数据,您将遇到分段错误...

如果您没有将refcheck 设置为False,则不会发生这种情况,但它也不允许您在函数内进行修改。所以是的,它可以做到,但你不应该这样做:遵循 Entropiece 的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 2021-10-31
    相关资源
    最近更新 更多