python advanced 3.the python std lib by example –data structures

30
THE PYTHON STD LIB BY EXAMPLE – DATA STRUCTURES John Tuesday, June 7, 2022

Upload: john-zhang

Post on 19-Dec-2014

353 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Python advanced 3.the python std lib by example –data structures

THE PYTHON STD LIB BY EXAMPLE – DATA

STRUCTURESJohnMonday, April 10, 2023

Page 2: Python advanced 3.the python std lib by example –data structures

Overview of this class

• Python already include several standard programming data structures, such as list,tuple,dict and set.

• The collection module include other several data structures, such as Deque, defaultdict, OrderDict and namedtuple.

• For large amount of data, an array module is more efficient

Page 3: Python advanced 3.the python std lib by example –data structures

THE COLLECTION MODULE

Page 4: Python advanced 3.the python std lib by example –data structures

Data type Counter

• A container tracks how many times equivalent values are added.

Page 5: Python advanced 3.the python std lib by example –data structures

Update() method of Counter

• The count value will be increased based on the new data, rather than replaced.

Page 6: Python advanced 3.the python std lib by example –data structures

More functions in Counter class

• c[i]: return the count of i.• c.elements(): return an iterator• c.most_common(3): produce a list in order of

frequency• c.update({‘a’:1,’b’:5}): The count value will be

increased based on the new data, rather than replaced.

Page 7: Python advanced 3.the python std lib by example –data structures

Data type defaultdict

Similar as dict.It can let the caller specify the default value when container is initialized.

Page 8: Python advanced 3.the python std lib by example –data structures

Data type deque

• Support adding and removing elements from either end.

Page 9: Python advanced 3.the python std lib by example –data structures

More method in deque

• Method extend,extendleft• Method append,appendleft• Method pop,popleft• Method rotate: rotate the deque to the right

direction

Page 10: Python advanced 3.the python std lib by example –data structures

Data type namedtuple

• Similar as regular tuple, but can use instance.attr to access the elements

Page 11: Python advanced 3.the python std lib by example –data structures

Data type OrderDict

• A dictionary subclass that remember the order.

Page 12: Python advanced 3.the python std lib by example –data structures

Data type heapq

• A min-heap: a tree like data structure that the parent should be less than or equal to its children.

• Binary heap use a list or array represent it.The children of elementN is 2*N+1 and 2*N+2(zero based indexes).

Page 13: Python advanced 3.the python std lib by example –data structures

Function of heapq class

Heap =[]•method: heapq.heappush(Heap,n): add n into heap.•Method: heapq.heapify(list): sort list as a heap•Method heapq.heappop(Heap): pop the smallest item•Method heapq.heapreplace(heap,n): replace the smallest item with n.

Page 14: Python advanced 3.the python std lib by example –data structures

Data type bitsect

• Purpose is maintain a list in sorted order without having to call sort each time adding new item.

Page 15: Python advanced 3.the python std lib by example –data structures

Method of bitsect

• Method insort(alias of insort_right): insert after the existing value

• Method insort_left: insert before the existing value.

• Method bisect(alias bisect_right): return the position after the existing value.

• Method bisect_left: return the position before the existing value.

Page 16: Python advanced 3.the python std lib by example –data structures

Data type Queue

• The Queue module provides FIFO (first in, first out) data structure suitable for multithreaded programming.

Page 17: Python advanced 3.the python std lib by example –data structures

Data type LIFO Queue (Stack)

• LifoQueue use LIFO (Last in, first out) (normally we call it stack data structure).

Page 18: Python advanced 3.the python std lib by example –data structures

Data type PriorityQueue

• The process order is based on characteristics of those items, rather than the order.

Example code see here:Import Queueq = Queue.PriorityQueue()

Page 19: Python advanced 3.the python std lib by example –data structures

THE WEAKREF MODULE

Page 20: Python advanced 3.the python std lib by example –data structures

Brief introduction

• The weakref module support weak references to objects.

• A normal reference increase the reference count on the object and prevent it from being garbage collected.

Page 21: Python advanced 3.the python std lib by example –data structures

Example of weakref

• Class ref to create a weak reference• Ref will return None

if obj is deleted.

Page 22: Python advanced 3.the python std lib by example –data structures

THE COPY MODULE

Page 23: Python advanced 3.the python std lib by example –data structures

Brief introduction

• Provide functions for duplicating objects using shallow or deep copy semantics

• The copy module include two functions, copy() and deepcopy().

Page 24: Python advanced 3.the python std lib by example –data structures

Shallow copy: copy()

• A new container populated with references to the contents of the original object.

Page 25: Python advanced 3.the python std lib by example –data structures

Deep copies: deepcopy()

• A New container populated with copies of the contents of the original object.

• It is possible to control hwo copies are made using the __copy__() and __deepcopy__() special methods.

Page 26: Python advanced 3.the python std lib by example –data structures

THE PPRINT MODULE

Page 27: Python advanced 3.the python std lib by example –data structures

Quick example of pprint

• The pprint module contains a “pretty printer”

Page 28: Python advanced 3.the python std lib by example –data structures

Work with custom classes

• If class define __repr__() method, pprint() can also work.

Page 29: Python advanced 3.the python std lib by example –data structures

More options

• Option depth: control the print depth for very deep data strctures.>>> pprint(data,depth=1)

• Option width: default output width is 80 columns.>>> pprint(data, width=5)

Page 30: Python advanced 3.the python std lib by example –data structures

Reference

• Source code https://bitbucket.org/qzhang03022/py_stdlib_by_example