question on list comprehensions

Frohnhofer, James james.frohnhofer at csfb.com
Thu Oct 14 11:01:01 EDT 2004


Not sure what you're trying to accomplish.  Either there's a typo or you need
to unsimplify the problem a bit (or both).

As far as I can tell

res=[49995000.0]

is equivalent to 

res=[0]
for i in arange(10000):
  res[0]=res[0]+i

> -----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 10:37 AM
> To: python-list at python.org
> Subject: question on list comprehensions
> 
> 
> Hi,
> 
> I need to replace the following loop with a list comprehension:
> 
> res=[0]
> for i in arange(10000):
>  res[0]=res[0]+i
> 
> 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.
> 
> I saw this article at ASPN:
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204297
> 
> def thislist():
>     """Return a reference to the list object being constructed by the
>     list comprehension from which this function is called. Raises an
>     exception if called from anywhere else.
>     """
>     import sys
>     d = sys._getframe(1).f_locals
>     nestlevel = 1
>     while '_[%d]' % nestlevel in d:
>         nestlevel += 1
>     return d['_[%d]' % (nestlevel - 1)].__self__
> 
> Could the list comprehension include something like 
> thislist().pop(0), to be
> called when len(thislist)>1? (I think this could work, but am having
> trouble with the syntax.) Or is there a better way to 
> approach the problem?
> 
> Thank you,
> 
> Darren
> -- 
> 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