#! /usr/bin/env python3 from collections import * print ("Counter") lst = ['red', 'blue', 'red', 'green', 'blue', 'blue'] cnt = Counter(lst) print (lst) print (cnt) print ("Total: " + str(cnt.total())) print ("\nMore counters, like sets") c = Counter(a=3, b=1) d = Counter(a=1, b=2) print ("c: ", c) print ("d: ", d) print ("c + d: ", c + d) # add two counters together: c[x] + d[x] print ("c - d: ", c - d) # subtract (keeping only positive counts) print ("c & d: ", c & d) # intersection: min(c[x], d[x]) print ("c | d: ", c | d) # union: max(c[x], d[x]) print ("c == d: ", c == d) # equality: c[x] == d[x] print ("c <= d: ", c <= d) # inclusion: c[x] <= d[x] print ("c == c: ", c == c) # equality: c[x] == c[x] print ("\nNamedTuple") Point = namedtuple('Point', ['x', 'y']) p = Point(11, y=22) # instantiate with positional or keyword arguments print (p) # readable __repr__ with a name=value style print (p[0] + p[1]) # indexable like the plain tuple (11, 22) x, y = p # unpack like a regular tuple print ("x, y: " , x, y) print ("p.x + p.y: ", p.x + p.y)# fields also accessible by name