一般には、bisect() 関数は数値データを分類するのに役に立ちます。 この例では、bisect() を使って、(たとえば)順序のついた数値の区切り点 の集合に基づいて、試験全体の成績の文字を調べます。 区切り点は 85 以上は `A'、 75..84 は `B'、などです。
>>> grades = "FEDCBA" >>> breakpoints = [30, 44, 66, 75, 85] >>> from bisect import bisect >>> def grade(total): ... return grades[bisect(breakpoints, total)] ... >>> grade(66) 'C' >>> map(grade, [33, 99, 77, 44, 12, 88]) ['E', 'A', 'B', 'D', 'F', 'A']
bisect モジュールは Queue モジュールと一緒に使って、優先順序つき待ち行列を実装 することができます。 (Fredrik Lundh の好意による例です):
import Queue, bisect class PriorityQueue(Queue.Queue): def _put(self, item): bisect.insort(self.queue, item) # 使い方 queue = PriorityQueue(0) queue.put((2, "second")) queue.put((1, "first")) queue.put((3, "third")) priority, value = queue.get()