1. 在處理雜湊表時,可以使用dict1={} ,比較方便
2. 瞭解 Counter
>>> from collections import Counter >>> ans = [1, 2, 3, 1] >>> dic = Counter(ans) >>> dic Counter({1: 2, 2: 1, 3: 1}) >>>
瞭解 defaultdict , 它 的引數可以是list,也可以是int
>>> from collections import defaultdict >>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)] >>> d = defaultdict(list) >>> for k,v in s: ... d[k].append(v) ... >>> d defaultdict(<class 'list'>, {'yellow': [1, 3], 'blue': [2, 4], 'red': [1]}) >>> d.items() dict_items([('yellow', [1, 3]), ('blue', [2, 4]), ('red', [1])]) >>>