If you can make f work in-place then you can just call map(f, [a, b, c, d]):
def f(arr):
   arr *= 2

Otherwise, you can:
- Work with a list instead (a_b_c_d = map(f, a_b_c_d), with a_b_c_d = [a, b, c, d], but this won't update the local definitions of a, b, c, d).
- Use locals():
for x in ('a', 'b', 'c', 'd'):
    locals()[x] = f(eval(x))

-=- Olivier

2011/9/12 David Froger <david.froger@gmail.com>
Hy everybody,

I'm wondering what is the (best) way to apply the same function to multiple

For example, in the following code:

from numpy import *

def f(arr):
    return arr*2

a = array( [1,1,1] )
b = array( [2,2,2] )
c = array( [3,3,3] )
d = array( [4,4,4] )

a = f(a)
b = f(b)
c = f(c)
d = f(d)

I would like to replace :

a = f(a)
b = f(b)
c = f(c)
d = f(d)

with something like that, but which really modify a,b,c and d:

for x in [a,b,c,d]:
   x = f(x)

So having something like a pointer on the arrays.

Thanks for your help !

NumPy-Discussion mailing list