[Tutor] synchronized enumeration
Kent Johnson
kent37 at tds.net
Fri Dec 16 17:42:42 CET 2005
Chris or Leslie Smith wrote:
> Has anyone else run into the desire to synchronize the indices that are being used during an enumeration with the true indices of the list that is being enumerated when you use a slice of the list?
>
> e.g. more than a couple of times, I want to use the enumeration function, but I don't want to start at the beginning of a list. I do something like:
>
> ###
> for i, x in enumerate(aList[3:]):
> pass #do something with the index and or x
>
> ###
>
> Of course, if I do something with x in this case, there is no problem, but (often enough) I forget that the index 0 that is returned by enumerate is actually corresponding to index 3 (b/c that's where I started the slice).
>
> What I would love to see--and is there a chance of this being considered--is something like the following behavior for enumerate:
>
> ###
> def enumerate(l, start=None, stop=None, step=None, alwaysPos = False):
> if step==None:step=1
> if start==None:
> if step<0:
> start=-1
> else:
> start=0
> for i, dat in enumerate(l[start:stop:step]):
> j = i*step+start
> if alwaysPos and j<0: j+=len(l)
> yield j, dat
>
> for i, x in enumerate(range(5),3):
> print i, x
> ###
>
> which gives output:
>
> 3 3
> 4 4
>
> rather than enumerate(range(5)[3:])'s output of
>
> 0 3
> 1 4
>
> Any thoughts?
Take a look at this thread on c.l.py for some discussion and possibilities. The part you
are interested in starts around message 14.
http://groups.google.com/group/comp.lang.python/browse_frm/thread/ab1658dca4023e2b?hl=en&
If you seriously want this to be considered for inclusion in Python you will need to
broaden the discussion beyond this list. PEP 1 outlines the official process for getting
something added to Python; it doesn't show the unofficial process which usually seems to
involve a discussion on c.l.py or python-dev.
http://www.python.org/peps/pep-0001.html
Kent
More information about the Tutor
mailing list