Your SlideShare is downloading. ×
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

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

490

Published on

Published in: Technology
0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
490
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
62
Comments
0
Likes
8
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • This template can be used as a starter file for presenting training materials in a group setting.
    Sections
    Right-click on a slide to add sections. Sections can help to organize your slides or facilitate collaboration between multiple authors.
    Notes
    Use the Notes section for delivery notes or to provide additional details for the audience. View these notes in Presentation View during your presentation.
    Keep in mind the font size (important for accessibility, visibility, videotaping, and online production)
    Coordinated colors
    Pay particular attention to the graphs, charts, and text boxes.
    Consider that attendees will print in black and white or grayscale. Run a test print to make sure your colors work when printed in pure black and white and grayscale.
    Graphics, tables, and graphs
    Keep it simple: If possible, use consistent, non-distracting styles and colors.
    Label all graphs and tables.
  • Transcript

    • 1. THE PYTHON STD LIB BY EXAMPLE – DATA STRUCTURES John Saturday, December 21, 2013
    • 2. 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
    • 3. THE COLLECTION MODULE
    • 4. Data type Counter • A container tracks how many times equivalent values are added.
    • 5. Update() method of Counter • The count value will be increased based on the new data, rather than replaced.
    • 6. 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.
    • 7. Data type defaultdict Similar as dict.It can let the caller specify the default value when container is initialized.
    • 8. Data type deque • Support adding and removing elements from either end.
    • 9. More method in deque • • • • Method extend,extendleft Method append,appendleft Method pop,popleft Method rotate: rotate the deque to the right direction
    • 10. Data type namedtuple • Similar as regular tuple, but can use instance.attr to access the elements
    • 11. Data type OrderDict • A dictionary subclass that remember the order.
    • 12. 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 element N is 2*N+1 and 2*N+2( zero based indexes).
    • 13. 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.
    • 14. Data type bitsect • Purpose is maintain a list in sorted order without having to call sort each time adding new item.
    • 15. 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.
    • 16. Data type Queue • The Queue module provides FIFO (first in, first out) data structure suitable for multithreaded programming.
    • 17. Data type LIFO Queue (Stack) • LifoQueue use LIFO (Last in, first out) (normally we call it stack data structure).
    • 18. Data type PriorityQueue • The process order is based on characteristics of those items, rather than the order. Example code see here: Import Queue q = Queue.PriorityQueue()
    • 19. THE WEAKREF MODULE
    • 20. 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.
    • 21. Example of weakref • Class ref to create a weak reference • Ref will return None if obj is deleted.
    • 22. THE COPY MODULE
    • 23. Brief introduction • Provide functions for duplicating objects using shallow or deep copy semantics • The copy module include two functions, copy() and deepcopy().
    • 24. Shallow copy: copy() • A new container populated with references to the contents of the original object.
    • 25. 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.
    • 26. THE PPRINT MODULE
    • 27. Quick example of pprint • The pprint module contains a “pretty printer”
    • 28. Work with custom classes • If class define __repr__() method, pprint() can also work.
    • 29. 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)
    • 30. Reference • Source code https://bitbucket.org/qzhang03022/py_stdlib_by

    ×