Iterating with Python?

Using map seems to be marginally quicker than a list comprehension.

Up vote 1 down vote favorite share g+ share fb share tw.

I have two arrays and from these two I have to create another one in this way: for I in arange(0,len(second_array),1): third_arrayi = my_function(first_arrayi,second_arrayi) Here my_function is a procedure which takes two scalars as inputs and then return another scalar. My problem is that the arrays I usually work with are huge and so the above loops takes forever. Is there a way to avoid the loop but still filling the array third_array the way I want?

Python iteration link|improve this question edited Mar 26 at 19:35animuson10.3k51740 asked Jan 9 at 13:30Matteo1788 79% accept rate.

Using map seems to be marginally quicker than a list comprehension: import cProfile, numpy as np from operator import add A = np.random. Rand(1000000) B = np.random. Rand(1000000) >>> cProfile.

Run('C = map(add, A, B)') 3 function calls in 0.693 seconds >>> cProfile. Run('C = a+b for a,b in izip(A,B)') 2 function calls in 0.765 seconds >>> cProfile. Run('for I in np.

Arange(0,len(B),1): Ci = Ai+Bi') 4 function calls in 1.971 seconds But as @larsmans says, using a vectorized solution will be much quicker: >>> cProfile. Run('C = A + B') 2 function calls in 0.005 seconds.

Since you're using arange, I take it you're using NumPy. Try to rewrite my_function so that it takes two arrays instead of two scalar values and use vectorized operations.

I don't really understand your question. But you can do it a bit more simply. In Python 3: third_array = my_function(a, b) for a, be in zip(first_array, second_array) In Python 2, it's better to use from itertools import izip third_array = my_function(a, b) for a, be in izip(first_array, second_array).

Since you're already using NumPy, it may be worth exploring universal functions (ufunc) and numpy.frompyfunc(). In 1: import numpy as np In 2: first_array = np. Arange(10) In 3: second_array = np.

Arange(10, 20) In 5: def halfsum(a, b): return (a + b) / 2.0 ...: In 7: halfsum_ufunc = np. Frompyfunc(halfsum, 2, 1) In 8: halfsum_ufunc(first_array, second_array) Out8: array(5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, dtype=object) One caveat is that frompyfunc-created ufuncs always return PyObject arrays. I am not sure if there's a way around that.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions


Thank You!
send