Can I overload the compare (cmp()) function for a Lists ([]) index function?

irstas at gmail.com irstas at gmail.com
Fri Sep 28 14:06:07 EDT 2007


On Sep 28, 8:30 pm, xkenneth <xkenn... at gmail.com> wrote:
> Looking to do something similair. I'm working with alot of timestamps
> and if they're within a couple seconds I need them to be indexed and
> removed from a list.
> Is there any possible way to index with a custom cmp() function?
>
> I assume it would be something like...
>
> list.index(something,mycmp)
>
> Thanks!

Wouldn't it be enough to get the items that are "within a couple of
seconds" out of the list and into another list. Then you can process
the other list however you want. Like this:

 def isNew(x):
     return x < 5

 data = range(20)
 print data
 out, data = filter(isNew, data), filter(lambda x: not isNew(x), data)
 print out, data

Why do you want to use 'index'?

Your suggestion "list.index.__cmp__ = mycmp" certainly doesn't do
anything good. In fact, it just fails because the assignment is
illegal.. I don't think any documentation suggests doing that, so why
are you even trying to do that? It's just not a good idea to invent
semantics and hope that they work, in general.




More information about the Python-list mailing list