|
| 1 | +from collections import * |
| 2 | + |
| 3 | +# nn = ChainMap({'a':1, 'b':2}, {'c':3, 'd':4}) |
| 4 | + |
| 5 | +count = Counter() |
| 6 | +for i in ['red', 'blue', 'red']: |
| 7 | + count[i] += 1 |
| 8 | + |
| 9 | +print count |
| 10 | +print count.elements() |
| 11 | + |
| 12 | +import re |
| 13 | +# words = re.findall(r'\w+', open('hamlet.txt').read().lower()) |
| 14 | +# Counter(words).most_common(10) |
| 15 | + |
| 16 | +""" |
| 17 | +>>> c = Counter(a=2, b=-4) |
| 18 | +>>> +c |
| 19 | +Counter({'a': 2}) |
| 20 | +>>> -c |
| 21 | +Counter({'b': 4}) |
| 22 | +
|
| 23 | +>>> c = Counter(a=3, b=1) |
| 24 | +>>> d = Counter(a=1, b=2) |
| 25 | +>>> c + d # add two counters together: c[x] + d[x] |
| 26 | +Counter({'a': 4, 'b': 3}) |
| 27 | +>>> c - d # subtract (keeping only positive counts) |
| 28 | +Counter({'a': 2}) |
| 29 | +>>> c & d # intersection: min(c[x], d[x]) |
| 30 | +Counter({'a': 1, 'b': 1}) |
| 31 | +>>> c | d # union: max(c[x], d[x]) |
| 32 | +Counter({'a': 3, 'b': 2}) |
| 33 | +
|
| 34 | +""" |
| 35 | +mm = deque() |
| 36 | +mm.append('a') |
| 37 | +mm.append('b') |
| 38 | +mm.append('c') |
| 39 | +mm.append('d') |
| 40 | +print mm |
| 41 | + |
| 42 | +mm.appendleft('e') |
| 43 | +print mm |
| 44 | + |
| 45 | +print mm.count('a') |
| 46 | +print mm.pop() |
| 47 | + |
| 48 | +print mm |
| 49 | +mm.rotate(2) |
| 50 | +print mm |
| 51 | + |
| 52 | +""" |
| 53 | +def tail(filename, n=10): |
| 54 | + 'Return the last n lines of a file' |
| 55 | + with open(filename) as f: |
| 56 | + return deque(f, n) |
| 57 | +
|
| 58 | +
|
| 59 | +>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)] |
| 60 | +>>> d = defaultdict(list) |
| 61 | +>>> for k, v in s: |
| 62 | +... d[k].append(v) |
| 63 | +... |
| 64 | +>>> sorted(d.items()) |
| 65 | +[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])] |
| 66 | +
|
| 67 | +
|
| 68 | +>>> # Basic example |
| 69 | +>>> Point = namedtuple('Point', ['x', 'y']) |
| 70 | +>>> p = Point(11, y=22) # instantiate with positional or keyword arguments |
| 71 | +>>> p[0] + p[1] # indexable like the plain tuple (11, 22) |
| 72 | +33 |
| 73 | +>>> x, y = p # unpack like a regular tuple |
| 74 | +>>> x, y |
| 75 | +(11, 22) |
| 76 | +>>> p.x + p.y # fields also accessible by name |
| 77 | +33 |
| 78 | +>>> p # readable __repr__ with a name=value style |
| 79 | +Point(x=11, y=22) |
| 80 | +
|
| 81 | +
|
| 82 | +>>> # regular unsorted dictionary |
| 83 | +>>> d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2} |
| 84 | +
|
| 85 | +>>> # dictionary sorted by key |
| 86 | +>>> OrderedDict(sorted(d.items(), key=lambda t: t[0])) |
| 87 | +OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)]) |
| 88 | +
|
| 89 | +>>> # dictionary sorted by value |
| 90 | +>>> OrderedDict(sorted(d.items(), key=lambda t: t[1])) |
| 91 | +OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)]) |
| 92 | +
|
| 93 | +>>> # dictionary sorted by length of the key string |
| 94 | +>>> OrderedDict(sorted(d.items(), key=lambda t: len(t[0]))) |
| 95 | +OrderedDict([('pear', 1), ('apple', 4), ('orange', 2), ('banana', 3)]) |
| 96 | +""" |
| 97 | + |
| 98 | + |
0 commit comments