Created
June 20, 2018 04:29
-
-
Save a3linux/644b2928b95ba2bcd7432335e9b9b702 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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