今天同事說到了一個python的排序模塊bisect濒蒋,覺得挺有趣的,跟大家分享分享。
先看看模塊的結(jié)構(gòu):

前面五個屬性大家感興趣可以打出來看看數(shù)值沪伙,這里就不介紹了瓮顽。
先說明的是,使用這個模塊的函數(shù)前先確保操作的列表是已排序的围橡。

先看看 insort 函數(shù):

其插入的結(jié)果是不會影響原有的排序暖混。
再看看 bisect 函數(shù):

其目的在于查找該數(shù)值將會插入的位置并返回,而不會插入翁授。
接著看 bisect_left 和 bisect_right 函數(shù)拣播,該函數(shù)用入處理將會插入重復(fù)數(shù)值的情況,返回將會插入的位置:

其對應(yīng)的插入函數(shù)是 insort_left 和 insort_right :

可見收擦,單純看其結(jié)果的話贮配,兩個函數(shù)的操作結(jié)果是一樣的,其實插入的位置不同而已塞赂。