【《流畅的python》2.8节学习笔记】

发布于:2024-07-05 ⋅ 阅读:(130) ⋅ 点赞:(0)

前言

本文为《流畅的python》的2.8节的学习笔记。

1.bisect .besect函数


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]

#{0:2d}: 第0个参数以2位整形数
#{1:2d}: 第1个参数以2位整形数
#{2}{0:2d}:显示第2个数据+第0个参数以2位整形数
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))

#terminal window to enter 'python 2_point_7.py left' to select bisect_fn = bisect.bisect_left
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))
demo(bisect_fn)
>>>
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

说明:
此例用于展示bisect(haystack, needle)函数的用法,其作用是在haystack(干草垛)里搜索needle(针)的位置。
Demo函数遍历NEEDLES获取每个被查找元素,使用bisect函数查找该元素在HAYSTACK中的位置,并以此位置打印。
bisect函数实际就是bisect_right 函数,还有一个bisect_left 函数返回的插入位置位于与它相等的元素的前面,而bisect_right 返回的插入位置则是与它相等的元素的后面。
函数入口处(main),通过sys.argv接收本次运行bisect_left函数还是bisect函数,二者的实际差异可以通过以下两次打印结果看出来。

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
#####################################################
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

2.bisect .insort函数


import bisect
import random
import sys

my_list = []
for i in range(10):
    new_item = random.randrange(20)
    bisect.insort(my_list, new_item)
print('%2d ->' % new_item, my_list) 
>>>
0 -> [0]
14 -> [0, 14]
 6 -> [0, 6, 14]
11 -> [0, 6, 11, 14]
 9 -> [0, 6, 9, 11, 14]
 3 -> [0, 3, 6, 9, 11, 14]

说明:
此例用于说明insort函数,insort(seq, item) 把变量item 插入到序列seq 中,并能保持seq 的升序顺序。

总结

本小节主要讲bisect模块的两个函数。bisect . bisect函数和bisect .insort函数,bisect . bisect函数用于查找元素在序列里的位置,bisect .insort函数用于将某元素插入序列中并保持升序。


网站公告

今日签到

点亮在社区的每一天
去签到