A couple of quick questions

Fredrik Lundh fredrik at pythonware.com
Mon May 24 16:54:09 EDT 1999


<cbarker at jps.net> wrote:
> 1) loop a known number of times, without creating a list of indexes:
> 
> for i in range(N):
> # do something
> 
> works just fine, unless N is very large. if it's large, it wastes
> memory, if it's very large it can be impossible!

for i in xrange(Huge_N):
    # do something

> 2) Is there a "case" construct?

answer 1: hey, shouldn't you have read the faq before
posting? ;-)

answer 2: no.  if you need more than a few elif state-
ments, use a dispatch table instead.  or use dynamic
lookups.  or revise your class design.  etc.

see the newsgroup archives for more tips and tricks,
e.g. http://www.deja.com/getdoc.xp?AN=439589505

> 3) lists of indexes:
> 
> let's say I have a list of the indexes of another list that I want. Is
> there a way to return the corresponding elements without a loop?

I'm pretty sure someone will post an obscure oneliner for this,
but if you're comparing Python with Matlab, you should take a
serious look at the numerical extensions:

    http://www.python.org/topics/scicomp/

iirc, the function you're looking for is called "take".

</F>





More information about the Python-list mailing list