[issue4356] Add "key" argument to "bisect" module functions

Rémi Lapeyre report at bugs.python.org
Sun May 26 10:25:52 EDT 2019


Rémi Lapeyre <remi.lapeyre at henki.fr> added the comment:

Can you not use functools.cmp_to_key() for this?


Here's an example:

>>> import bisect, functools
>>> l = [('f', 5), ('d', 3), ('c', 2), ('b', 1), ('a', 0)]
>>> def cmp(a, b):
...     if a > b: return -1
...     if a < b: return 1
...     return 0
... 
>>> bisect.bisect(l, ('e', 4), key=functools.cmp_to_key(cmp))
1
>>> l
[('f', 5), ('d', 3), ('c', 2), ('b', 1), ('a', 0)]
>>> bisect.insort(l, ('e', 4), key=functools.cmp_to_key(cmp))
>>> l
[('f', 5), ('e', 4), ('d', 3), ('c', 2), ('b', 1), ('a', 0)]

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue4356>
_______________________________________


More information about the Python-bugs-list mailing list