0

I a function described as follows (very simplified version):

def my_func(*args):
    c = other_func(args)
    return c

And other_func is defined as:

def other_func(a, b):
    c = a + b
    return c

I have also two numpy arrays:

a = [[1.] [2.]]
b = [[2.] [5.]]

I want to pass a and b to my_func and retrieve them exactly as I passed them: The result I want is :

c = my_func(a, b)

With :

c = [[3.] [7.]]

But when I call my_func like above, I get this error:

TypeError: other_func missing 1 required positional argument: 'b'

I think the problem is that my_func is not able to unpack the data. I looked at an almost similar topic (link below) but It doesn't help me enough to fix my problem. Also, I don't want to use a Loop, it will not be practical for my work.

Link : How to split list and pass them as separate parameter?

Can anyone help me solve this?

Thank you in advance.

narutoArea51
  • 141
  • 11
  • **you are passing a single argument**. It is up to **you** to unpack the arguments, you need to use `_1, _2 = other_func(*args)` – juanpa.arrivillaga Aug 31 '21 at 19:26
  • `other_func` takes two arguments; that's what the function definition specifies. You have to give two, either as `a,b` tuple or with the `*args` unpacking. With `a+b`, the arguments could be scalars or arrays (or even lists). In the real case, are there constraints on those arguments? – hpaulj Aug 31 '21 at 20:09

3 Answers3

4

Change the line

c = other_func(args)

to

c = other_func(*args)
NGilbert
  • 485
  • 6
  • 12
  • This won't work as it will add together the two arrays not add together the items within each array – Lecdi Aug 31 '21 at 18:41
  • 2
    What do you mean by "add together"? `args` is a tuple, `*args` unpacks it into the two original arguments passed to `my_func`, to pass on to `other_func`. – chepner Aug 31 '21 at 19:22
  • 1
    @narutoArea51 it absolutely *does* work. If you say it doesn't work then you must demonstrate how with a [mcve] – juanpa.arrivillaga Aug 31 '21 at 19:27
  • @juanpa.arrivillaga I just edited my post to the original problem. If you copy and paste the code and run it, you will see that it does not give the expected result. – narutoArea51 Aug 31 '21 at 19:34
  • @narutoArea51 I'm assuming because you eventually accepted this answer, that in fact, it *does work* – juanpa.arrivillaga Aug 31 '21 at 22:30
  • @juanpa.arrivillaga I didn't realise adding two np arrays added the numbers within them I thought it would work like a normal list where it just appends the items from one onto the other so [1, 2, 2, 5]. But yes, this does work. – Lecdi Sep 01 '21 at 08:00
  • @juanpa.arrivillaga yes, you're right. It works, but a bit oddly. The data is processed as mentioned by Lecdi just above. – narutoArea51 Sep 02 '21 at 10:07
2

Given these 2 (2,1) arrays, a+b produces what you want:

In [776]: a = np.array([[1.], [2.]])
     ...: b = np.array([[2.], [5.]])
In [777]: a+b
Out[777]: 
array([[3.],
       [7.]])
In [778]: def my_func(a,b):
     ...:     return a+b
     ...: 
In [779]: my_func(a,b)
Out[779]: 
array([[3.],
       [7.]])

There is a certain ambiguity, since a matrix formed from them is symmetric

In [780]: np.concatenate((a,b), axis=1)
Out[780]: 
array([[1., 2.],
       [2., 5.]])

Adding rows or columns both produce the [3,7] values.

Overall your question is poorly specified

hpaulj
  • 221,503
  • 14
  • 230
  • 353
0

You want to iterate over the args list and unpack and input each array separately into other_func like this:

results = []
for arg in args:
    results.append(other_func(*arg))
Lecdi
  • 2,189
  • 2
  • 6
  • 20