Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save a3linux/644b2928b95ba2bcd7432335e9b9b702 to your computer and use it in GitHub Desktop.
Save a3linux/644b2928b95ba2bcd7432335e9b9b702 to your computer and use it in GitHub Desktop.
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment