[Tutor] list.index method

amoreira@mercury.ubi.pt amoreira@mercury.ubi.pt
Fri, 12 May 2000 16:22:09 +0000


Thanks for your reply!

Yes, I did that as well (or something very much like it), but still, it has
the possible drawback that it
is a method implemented in python, instead of beeing builtin at the
C-level. I don't really know if
this means a much slower execution, but I suspect so. Anyway, this is just
a matter of aestetics
(how do we spell this in english?). I'm used to fortran arrays, where you
have to search the array
by iterating over the array index (fortran90/95 very much improved this,
with array syntax), so I
know how to solve my problem, but in an very ugly way... At least for
python standards. Extending
UserList  is much more beautifull, but I keep wondering why the list.index
method was designed as
it is. Optimization issues, perhaps?

But please, don't take my mutterings as criticism of the language, or as
proposals to change it's
definition! I like it as it is, and I am taking a great deal of fun
learning it. At the same time, I'm also
learning OOP, wich is something completly out of my previous programming
experience.
And if I want raw speed, I can use numpy or get it done in fortran!

Again, thanks.
Cheers!
Ze Amoreira,
amoreira@mercury.ubi.pt




Emile van Sebille wrote:

> You can extend UserList and change index
> to get what you want:
>
> import UserList
>
> class MyList(UserList.UserList):
>   def index(self, item):
>     cont_from = 0
>     while cont_from <= len(self.data):
>       retval = self.data[cont_from:].index(item)
>       if self.data[cont_from + retval] is item:
>         return retval + cont_from
>       else:
>         cont_from = cont_from + retval + 1
> [...]