python bisect模組的使用

2021-06-01 20:49:49 字數 757 閱讀 5106

這個模組只有幾個函式,

一旦決定使用二分搜尋時,立馬要想到使用這個模組 

import bisect

l = [1,3,3,6,8,12,15]

x = 3

x_insert_point = bisect.bisect_left(l,x)  #在l中查詢x,x存在時返回x左側的位置,x不存在返回應該插入的位置..這是3存在於列表中,返回左側位置1

print x_insert_point

x_insert_point = bisect.bisect_right(l,x) #在l中查詢x,x存在時返回x右側的位置,x不存在返回應該插入的位置..這是3存在於列表中,返回右側位置3

print x_insert_point

x_insort_left = bisect.insort_left(l,x) #將x插入到列表l中,x存在時插入在左側

print l

x_insort_rigth = bisect.insort_right(l,x) #將x插入到列表l中,x存在時插入在右側    

print l

結果:1

3[1, 3, 3, 3, 6, 8, 12, 15]

[1, 3, 3, 3, 3, 6, 8, 12, 15]

實際使用中

bisect.insort_left與 bisect.insort_right 差別不大,作用基本相同 。。。

python bisect模組的使用

這個模組只有幾個函式,一旦決定使用二分搜尋時,立馬要想到使用這個模組 python view plain copy print import bisect l 1,3,3,6,8,12,15 x 3 x insert point bisect.bisect left l,x 在l中查詢x,x存在時返...

Python bisect模組的使用與原始碼分析

本文基於python3.7分析 bisect提供了六個方法 不難發現,bisect 方法和bisect right 方法以及insort 方法和insort right 方法功能一致,在下面原始碼分析中會有解釋 import bisect l 1,2,4,4,5 n 4 idx1 bisect.bi...

模組的使用,模組的搜尋路徑

模組的使用 1 什麼是模組 模組是一系列功能的集合體 常見的模組形式 自定義模組 第三方模組 內建模組 1 乙個module.py檔案就是乙個模組,檔名是module.py,而模組名是module 2 乙個包含有 init py檔案的資料夾也是模組 3 已被編譯為共享庫或dll的c或c 擴充套件 4...