question on list comprehensions

Frohnhofer, James james.frohnhofer at csfb.com
Thu Oct 14 18:01:12 CEST 2004


I have to admit, I'm still not sure I understand the problem, but is this what
you're trying to do?

>>>
res=[[0,1,2,3,4,5,6,7,8,9],[2,2,2,2,2,2,2,2,2,2],[0,-1,-2,-3,-4,-5,-6,-7,-8,-9]]

where in reality rather than a list of 3 lists of 10 elements, res would be a
list of 2500 lists of 2000 elements each.

>>> map(lambda x: x*x, 
      map(lambda tup: reduce(lambda x,y:x+y,tup),
        [ 
          [res[i][j] for i in range(0,len(res))] 
          for j in range(0,len(res[0]))
        ]
      )
    )
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4]

If that's what you're trying to do.  I have no idea about whether this is
faster than other methods, although it certainly seems to me to be less clear.
If you stored your data cross-sectionally, it would be much simpler.  (Again,
if this it what you're trying to do.)

> -----Original Message-----
> From: python-list-bounces+james.frohnhofer=csfb.com at python.org
> [mailto:python-list-bounces+james.frohnhofer=csfb.com at python.org]On
> Behalf Of Darren Dale
> Sent: Thursday, October 14, 2004 11:16 AM
> To: python-list at python.org
> Subject: Re: question on list comprehensions
> 
> 
> 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.
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 

==============================================================================
This message is for the sole use of the intended recipient. If you received
this message in error please delete it and notify us. If this message was
misdirected, CSFB does not waive any confidentiality or privilege. CSFB
retains and monitors electronic communications sent through its network.
Instructions transmitted over this system are not binding on CSFB until they
are confirmed by us. Message transmission is not guaranteed to be secure.
==============================================================================




More information about the Python-list mailing list