[Numpy-discussion] iterate over multiple arrays
Olivier Delalleau
shish@keba...
Mon Sep 12 06:00:00 CDT 2011
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
> arrays.
>
> 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 !
>
> David
> --
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20110912/030df7ec/attachment.html
More information about the NumPy-Discussion
mailing list