A question.

Alex Martelli aleaxit at yahoo.com
Tue Dec 12 09:52:00 EST 2000


"DeHa" <deha at poczta.onet.pl> wrote in message
news:915a7j$fe0$1 at panorama.wcss.wroc.pl...
>
> Alex Martelli napisa³(a) w wiadomo¶ci: <9156h002uh2 at news2.newsguy.com>...
> >"DeHa" <deha at poczta.onet.pl> wrote in message
> >news:9152a0$acb$1 at panorama.wcss.wroc.pl...
> >
> >first = range(1,100001)
>
> I try this, but it doesn't work

Of course; there are many other errors, some of which I pointed out.
But this was a circuitous (although not erroneous) way to work


> >> for i in first:
> >>     if i != []:
> >> #until end of the list
> >
> >i will never equal an empty-list, as first's items are just integers.
>
> Well, I don't konw why it happend, but when there were no such line the
> program doesn't work.

Your program doesn't work anyway (with or without that line)
because of several other issues.  But, again, this specific
line plays no role.


> >Dynamical, interpreted languages such as Python are
> >not *mostly* about performance -- but hundreds or even
> >thousands of times of slowdown/speedup can easily be
> >had in simple cases by paying a _little_ attention to
> >performance issues.  I think it can be worth it...
> >
>
> Thanks for advice. Next time I will try to find better performance
issues:)

Correctness-first: you'll notice I _first_ produced a
working version of your original idea (modifying the
list itself, physically deleting items known not to be
primes), and only later introduced the key optimization
(not alter the list itself as we go, but rather keep
a separate 'parallel' list of flags; which lets us
remove the list itself, as it happens, since the list
of flags contains the same information...).

The key _simplification_ that made both versions easier
to write was looping on indices rather than on values.

Sometimes looping on values is simpler, sometimes you
need indices, sometimes it's ideal to have both, and in
the latter case it may be worth doing stuff like:
    for index, value in zip(range(len(seq)),seq):
        # whatever
(it WOULD be nice to have this frequent idiom be
more naturally expressed in Python -- Lemburg's
"new builtins" include this one and quite a few
other handy ones -- but, oh well).


Alex






More information about the Python-list mailing list