列表工具-基于s57国际标准的电子海图显示与导航系统

11.7列列列表表表工工工具具具很多数据结构可能会用到内置列表类型。然而,有时可能需要不同性能代价的实现。 array模块提供了一个类似列表的array()对象,它仅仅是存储数据,更为紧凑。以下的示例演示了一个存储双字节无符号整数的数组(类型编码"H")而非存储16字节Python整数对象的普通正规列表: >>> from array import array >>> a = array(’H’, [4000, 10, 700, 22222]) >>> sum(a) 26932 >>> a[1:3] array(’H’, [10, 700]) collections模块提供了类似列表的deque()对象,它从左边添加(append)和弹出(pop)更快,但是在内部查询更慢。这些对象更适用于队列实现和广度优先的树搜索: >>> from collections import deque >>> d = deque(["task1", "task2", "task3"]) >>> d.append("task4") >>> print("Handling", d.popleft()) Handling task1 unsearched = deque([starting_node]) def breadth_first_search(unsearched): node = unsearched.popleft() for m in gen_moves(node): if is_goal(m): return m unsearched.append(m)除了链表的替代实现,该库还提供了bisect这样的模块以操作存储链表: >>> import bisect >>> scores = [(100, ’perl’), (200, ’tcl’), (400, ’lua’), (500, ’python’)] >>> bisect.insort(scores, (300, ’ruby’)) >>> scores [(100, ’perl’), (200, ’tcl’), (300, ’ruby’), (400, ’lua’), (500, ’python’)] heapq提供了基于正规链表的堆实现。最小的值总是保持在0点。这在希望循环访问最小元素但是不想执行完整堆排序的时候非常有用: >>> from heapq import heapify, heappop, heappush >>> data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0] >>> heapify(data) # rearrange the list into heap order >>> heappush(data, -5) # add a new entry >>> [heappop(data) for i in range(3)] # fetch the three smallest entries [-5, 0, 1] 11.7.列列列表表表工工工具具具89海量IT类学习资源尽在三通it学院www.santongit.com
pdf 文件大小:1.7MB