An Array of Sequences(2)

1. Managing Ordered Sequences with bisect

The bisect module offers two main functions --- bisect and insort --- that use the binary serach algorithm to quickly find and insert items in any sorted sequence.

Example 2-17. bisect finds insertion points for items in a sorted sequence.

import bisect
import sys

HAYSTACK = [1, 4, 5, 6, 8, 12, 15, 20, 21, 23, 23, 26, 29, 30]
NEEDLES = [0, 1, 2, 5, 8, 10, 22, 23, 29, 30, 31]

ROW_FMT = '{0:2d} @ {1:2d}    {2}{0:<2d}'


def demo(bisect_fn):
    for needle in reversed(NEEDLES):
        position = bisect_fn(HAYSTACK, needle)
        offset = position * '  |'
        print(ROW_FMT.format(needle, position, offset))


if __name__ == "__main__":

    if sys.argv[-1] == "left":
        bisect_fn = bisect.bisect_left
    else:
        bisect_fn = bisect.bisect

print('DEMO:', bisect_fn.__name__)
print('haystack ->', ' '.join("%2d" % n for n in HAYSTACK))     # 注意此处 join 的用法: join 中没有用列表[] 符号
demo(bisect_fn)


"""
# 输出: 脚本不加参数
NEO:ch2-array-of-sequences zhenxink$ python3 search_with_bisect.py 
DEMO: bisect
haystack ->  1  4  5  6  8 12 15 20 21 23 23 26 29 30
31 @ 14      |  |  |  |  |  |  |  |  |  |  |  |  |  |31
30 @ 14      |  |  |  |  |  |  |  |  |  |  |  |  |  |30
29 @ 13      |  |  |  |  |  |  |  |  |  |  |  |  |29
23 @ 11      |  |  |  |  |  |  |  |  |  |  |23
22 @  9      |  |  |  |  |  |  |  |  |22
10 @  5      |  |  |  |  |10
 8 @  5      |  |  |  |  |8 
 5 @  3      |  |  |5 
 2 @  1      |2 
 1 @  1      |1 
 0 @  0    0 
 
# 输出: 脚本加参数 left
NEO:ch2-array-of-sequences zhenxink$ python3 search_with_bisect.py left
DEMO: bisect_left
haystack ->  1  4  5  6  8 12 15 20 21 23 23 26 29 30
31 @ 14      |  |  |  |  |  |  |  |  |  |  |  |  |  |31
30 @ 13      |  |  |  |  |  |  |  |  |  |  |  |  |30
29 @ 12      |  |  |  |  |  |  |  |  |  |  |  |29
23 @  9      |  |  |  |  |  |  |  |  |23
22 @  9      |  |  |  |  |  |  |  |  |22
10 @  5      |  |  |  |  |10
 8 @  4      |  |  |  |8 
 5 @  2      |  |5 
 2 @  1      |2 
 1 @  0    1 
 0 @  0    0 
"""

Example 2-18: Given a test score, grades returns the corresponding letter grade. (bisect 的一个应用例子)

import bisect


def grade(score, breakpoints=[60, 70, 80, 90], grades='FDCBA'):
    i = bisect.bisect(breakpoints, score)
    return grades[i]


letter_grade = [grade(score) for score in [33, 99, 77, 70, 89, 90, 100]]

print(letter_grade)

# 输出:
# ['F', 'A', 'C', 'C', 'B', 'A', 'A']

insort(seq, item) inserts items into seq so as to keep seq in ascending order.

Example 2-19: Insort keeps a sorted sequence always sorted.

import bisect
import random

SIZE = 7
random.seed(1729)

my_list = []
for i in range(SIZE):
    new_item = random.randrange(SIZE * 2)
    bisect.insort(my_list, new_item)
    print("%2d ->" % new_item, my_list)

'''
# 输出:
10 -> [10]
 0 -> [0, 10]
 6 -> [0, 6, 10]
 8 -> [0, 6, 8, 10]
 7 -> [0, 6, 7, 8, 10]
 2 -> [0, 2, 6, 7, 8, 10]
10 -> [0, 2, 6, 7, 8, 10, 10]
'''

# bisect.insort keeps a sorted sequence always sorted.

bisect 模块参数链接:https://www.cnblogs.com/skydesign/archive/2011/09/02/2163592.html 

end

猜你喜欢

转载自www.cnblogs.com/neozheng/p/12151052.html
今日推荐