Python bisect模組,pythonbisect模組

來源:互聯網
上載者:User

Python bisect模組,pythonbisect模組

Python的bisect模組是內建模組,bisect模組實現了一個演算法用於插入元素到有序列表。

在一些情況下,這比反覆排序列表或構造一個大的列表再排序的效率更高。Bisect是二分法的意思,這裡使用二分法排序,將待插入的元素插入到合適的位置

 

bisect模組提供了如下幾個函數:

(下面函數中的lo和hi用於指定列表的區間,預設的是整個列表)


bisect.bisect_left(a, x, lo=0, hi=len(a))   返回將x插入到列表a中的索引位置,如果已有x,則返回第一個x的位置

bisect.bisect_right(a, x, lo=0, hi=len(a)) 返回將x插入到列表a中的索引位置,如果已有x,則返回最後一個x位置的下一個位置

bisect.bisect(a, x, lo=0, hi=len(a))         與bisect_right相同

 

bisect.insort_left(a, x, lo=0, hi=len(a))   將x插入到列表a中,如果已有x,插入到所有x的最左邊

bisect.insort_right(a, x, lo=0, hi=len(a)) 將x插入到列表a中,如果已有x,插入到所有x的最右邊

bisect.insort(a, x, lo=0, hi=len(a))         與insort_right相同

 

下面看個例子:

>>> import bisect>>> import random>>> mylist = list()>>> for i in range(10):num = random.randint(1,100)index = bisect.bisect_left(mylist, num)bisect.insort_left(mylist, num)print('num  ', str(num), '\tindex  ', str(index), '\tlist ', mylist)num   37 index   0 list  [37]num   31 index   0 list  [31, 37]num   82 index   2 list  [31, 37, 82]num   27 index   0 list  [27, 31, 37, 82]num   62 index   3 list  [27, 31, 37, 62, 82]num   83 index   5 list  [27, 31, 37, 62, 82, 83]num   83 index   5 list  [27, 31, 37, 62, 82, 83, 83]num   87 index   7 list  [27, 31, 37, 62, 82, 83, 83, 87]num   98 index   8 list  [27, 31, 37, 62, 82, 83, 83, 87, 98]num   99 index   9 list  [27, 31, 37, 62, 82, 83, 83, 87, 98, 99]>>> mylist = list()>>> for i in range(10):num = random.randint(1,100)index = bisect.bisect_right(mylist, num)bisect.insort_right(mylist, num)print('num  ', str(num), '\tindex  ', str(index), '\tlist ', mylist)num   85 index   0 list  [85]num   73 index   0 list  [73, 85]num   82 index   1 list  [73, 82, 85]num   36 index   0 list  [36, 73, 82, 85]num   80 index   2 list  [36, 73, 80, 82, 85]num   4 index   0 list  [4, 36, 73, 80, 82, 85]num   79 index   3 list  [4, 36, 73, 79, 80, 82, 85]num   6 index   1 list  [4, 6, 36, 73, 79, 80, 82, 85]num   64 index   3 list  [4, 6, 36, 64, 73, 79, 80, 82, 85]num   25 index   2 list  [4, 6, 25, 36, 64, 73, 79, 80, 82, 85]>>> mylist = list()>>> for i in range(10):num = random.randint(1,100)index = bisect.bisect(mylist, num)bisect.insort(mylist, num)print('num  ', str(num), '\tindex  ', str(index), '\tlist ', mylist)num   25 index   0 list  [25]num   28 index   1 list  [25, 28]num   81 index   2 list  [25, 28, 81]num   9 index   0 list  [9, 25, 28, 81]num   29 index   3 list  [9, 25, 28, 29, 81]num   66 index   4 list  [9, 25, 28, 29, 66, 81]num   34 index   4 list  [9, 25, 28, 29, 34, 66, 81]num   60 index   5 list  [9, 25, 28, 29, 34, 60, 66, 81]num   61 index   6 list  [9, 25, 28, 29, 34, 60, 61, 66, 81]num   3 index   0 list  [3, 9, 25, 28, 29, 34, 60, 61, 66, 81]




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.