Problem with the sort() function

Fuzzyman fuzzyman at gmail.com
Tue Feb 22 09:07:04 EST 2005


Sion Arrowsmith wrote:
> clementine <kashmira_v_phalak at yahoo.com> wrote:
> >Thanx Nick...I forgot to mention im using python 2.2 and along with
a host
> >of other things it doesnt seem to have the enumarate built in
function
> >:(:(:(...is it possible to replace it by something else? I dont
think
> >simulating it will be feasible....
>
> Here's one I prepared earlier:
>
> if sys.version_info < (2,3):
>     def enumerate(l):
>         return zip(range(len(l)), l)
>
> which will suck somewhat on large lists compared to being able to
> do it with iterators.
>

Iterators are available in python 2.2
class enumerate:
    def __init__(self, inlist):
        self.inlist = inlist
        self.index = 0

    def next(self):
        if self.index >= len(self.inlist): raise StopIteration
        thisone = self.inlist[self.index]
        self.index += 1
        return self.index-1, thisone

    def __iter__(self):
        return self

Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml

> --
> \S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
>   ___  |  "Frankly I have no feelings towards penguins one way or the
other"
>   \X/  |    -- Arthur C. Clarke
>    her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump
bump bump




More information about the Python-list mailing list