question on list comprehensions

Darren Dale dd55 at cornell.edu
Thu Oct 14 11:16:12 EDT 2004


Roberto Antonio Ferreira De Almeida wrote:

> Darren Dale wrote:
>> Hi,
>> 
>> I need to replace the following loop with a list comprehension:
>> 
>> res=[0]
>> for i in arange(10000):
>>  res[0]=res[0]+i
> 
> res[0] = (10000 * (10000-1))/2.0   ;-)
> 
>> In practice, res is a complex 2D numarray. For this reason, the regular
>> output of a list comprehension will not work: constructing a list of
>> every intermediate result will result in huge hits in speed and memory.
> 
> Why do you *need* to replace the for loop with a listcomp? Could you
> give more details about what you're doing with the complex array?
> 
> Roberto


OK. As usual, I am having trouble clearly expressing myself. Sorry about
that. Prepare for some physics:

I am simulating diffraction from an array of particles. I have to calculate
a complex array for each particle, add up all these arrays, and square the
magnitude of the result. If I do a for loop, it takes about 6-8 seconds for
a 2000 element array added up over 250 particles. In reality, I will have
2500 particles, or even 2500x2500 particles. 

The list comprehension takes only 1.5 seconds for 250 particles. Already,
that means the time has decreased from 40 hours to 10, and that time can be
reduced further if python is not constantly requesting additionaly memory
to grow the resulting list. 

I guess that means that I would like to avoid growing the list and popping
the previous result if possible, and just over-write the previous result.



More information about the Python-list mailing list