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
2. Array
If the list will only contain numbers, an array.array is more efficient than a list : array supports all mutable sequence operations(including .pop, .insert, and .extend), and additional methods for fast loading and saving such as .frombytes and .tofile .
Example 2-20. Creating , saving, and loading a large array of floats.
from array import array from random import random floats = array('d', (random() for i in range(10 ** 7))) # create an array of double-precision floats(typecode 'd') from any iterable # object -- in this case, a generator expression. print(floats[-1]) with open("floats.bin", "wb") as fp: floats.tofile(fp) floats2 = array('d') # create an empty array of doubles. with open("floats.bin", "rb") as fp: floats2.fromfile(fp, 10 ** 7) # array.fromfile(f, n) ---> Read n objects from the file object f and append them to the end of the array. print(floats2[-1]) print(floats == floats2) """ 输出结果: 0.31630082281843985 0.31630082281843985 True """
array 用法链接: https://blog.csdn.net/xc_zhou/article/details/88538793
end