Python simplest realization dichotomy

Bisect module previously used this time, mostly used to maintain an orderly queue. I did not expect to use it to achieve dichotomy.

Today, when do question the dichotomy needs to be done, write a bit cumbersome, or whether it is a recursive loop. I thought, python modules so much, should not readily available?

 

Then suddenly feel bisect this module Is it not possible to use it? Think for a few minutes, the feeling is certainly possible, but is thought to understand how to get (probably the last two weeks at home, home office Daisha). Simply to search a bit, really.

http://kuanghy.github.io/2016/06/14/python-bisect

 

The code is very simple.

def binary_search_bisect(lst, x): i = bisect_left(lst, x) if i != len(lst) and lst[i] == x: return i return None

 

This lovely friend has helped us somehow, he even helped us test and compare the recursive loop of bisect. There are even a relatively numpy.searchsorted.

 

Basically unless you are using numpy of ndarray, use bisect + version is the easiest and most efficient.

Guess you like

Origin www.cnblogs.com/symbiosis/p/12602742.html