Guest User

Untitled

a guest
Jun 21st, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. # Collections module user cases
  2.  
  3. * Counter() a dict with counter for example,
  4. cnt = Counter()
  5. cnt[key1] += 1
  6.  
  7. * namedtuple e.g. Point = namedtuple('Point', ['x', 'y'])
  8. p = Point(1,2)
  9. p.x
  10. p.y
  11. to access
  12. For some simple case that a Class is too big but we need a quick data structure.
  13.  
  14. * deque bi-directly queue. pop and push in bi-directions
  15. q = deque(['a', 'b', 'c'])
  16. q.append('x')
  17. q.appendleft('y')
  18. q should be ['y', 'a', 'b', 'c', 'x'] now
  19. append/pop /appendleft/popleft
  20.  
  21. * defaultdict
  22. dd = defaultdict(lambda: 'not defined')
  23. dd[k] if not defined, return not defined then
  24. defaultdict(list), defaultdict(dict), defaultdict(int) for default values by default types.
  25.  
  26. * OrderedDict - it is an ordered / FIFO
Add Comment
Please, Sign In to add comment