Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Collections module user cases
- * Counter() a dict with counter for example,
- cnt = Counter()
- cnt[key1] += 1
- * namedtuple e.g. Point = namedtuple('Point', ['x', 'y'])
- p = Point(1,2)
- p.x
- p.y
- to access
- For some simple case that a Class is too big but we need a quick data structure.
- * deque bi-directly queue. pop and push in bi-directions
- q = deque(['a', 'b', 'c'])
- q.append('x')
- q.appendleft('y')
- q should be ['y', 'a', 'b', 'c', 'x'] now
- append/pop /appendleft/popleft
- * defaultdict
- dd = defaultdict(lambda: 'not defined')
- dd[k] if not defined, return not defined then
- defaultdict(list), defaultdict(dict), defaultdict(int) for default values by default types.
- * OrderedDict - it is an ordered / FIFO
Add Comment
Please, Sign In to add comment