2

How to concatenate two numpy arrays inside a function and return it considering the following program

#!/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

The result of this code is :

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]

And I want :

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

How to modify the provided program to get the expected result (the 4 lines marked by # This line should not be modified should remain the same) ?

3
  • 2
    Vincent, I don't believe that what you asking for is quite possible here. You can pass a list into a function, add on elements, and the original list that was passed in will be modified. However, numpy arrays cannot grow or shrink like lists can. The concatenate function creates an entirely new object and this is why the original object, array([1,2,3]) is not changed. As Dave said, you will have to return the new array concatenated array object to use it. Commented Mar 4, 2013 at 16:35
  • I am not yet convinced that this is impossible. However, even if it is possible to modify myarray in-place, you certainly wouldn't want to combine that with it having a default value (myarray = np.zeros(0)). Commented Mar 4, 2013 at 16:42
  • 4
    I think you're suffering an XY problem. You have a problem, you think you know an approach which will solve it, and so you're asking questions about how to get your approach to work -- even if it doesn't fit nicely into Python's object model -- instead of asking how to solve your problem. Commented Mar 4, 2013 at 16:42

2 Answers 2

2

You should return the value

Modify the function like that:

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

and then you get the result like that

result = myfunction(myarray)
Sign up to request clarification or add additional context in comments.

4 Comments

I marked this line as not modifiable because how can I do that when I have 2 arrays to modify in a single function ? (and that is the case in my code)
What do you mean? You need to concatenate two arrays is that correct? concatenated = np.concatenate((myarray, data)) and then return the concatenated object
I have myarray1, myarray2, data1, data2 and I need to concatenate (myarray1 and data1) and (myarray2 and data2).
You can return multiple values per method, so you can do something like return array1,array2,array3 ... etc
1

You could do the following, but it can go very wrong:

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

And as you would expect:

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

But:

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

And if you try to modify any of the data in c you have a segmentation fault...

If you didn't set refcheck to False that wouldn't happen, but it wouldn't let you do the modification inside a function either. So yes, it can be done, but you shouldn't do it: follow Entropiece's method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.