PEP 276 Simple Iterator for ints (fwd)

Magnus Lie Hetland mlh at idi.ntnu.no
Wed Nov 14 23:12:41 CET 2001


"Lulu of the Lotus-Eaters" <mertz at gnosis.cx> wrote in message
news:mailman.1005708499.16461.python-list at python.org...
> David Eppstein wrote:
> >    for i in [n-1, n-2, ... 0]:
> >        for j in [i+1, i+2, ... n]:
> >            ...do something...
>
> James_Althoff at i2.com wrote:
> |Thanks for your interesting example.  I agree that your notation "[n-1,
> |n-2, ... 0]" is a very nice way of showing a sequence of integers.  I'm
not
> |sure how one would turn this into programming language syntax that would
be
> |as clear.
>
> Well... here's how Haskell does it:
>
>   [1 .. 4]         --> [1,2,3,4]
>   [4,3 .. 0]       --> [4,3,2,1,0]
>   [2,4 .. 10]      --> [2,4,6,8,10]
>   [0.0,0.3 .. 1.0] --> [0.0,0.3,0.6,0.9]
>
> It's hard to think of a better syntax for Python to use.

IMO it's really a pity that we don't have this sort of thing in Python,
including possibly a version without the brackets (for iterators,
perhaps? Or I guess tuples would be more natural, although less suited
for for loops):

  for i in 1 .. 10: print i

or

  for i in 0, 2 .. 20: print i

But I guess getting this in as part of the language isn't very realistic.

<sigh/>

--
Magnus Lie Hetland                           The Anygui Project
http://hetland.org                           http://anygui.sf.net






More information about the Python-list mailing list