- defaultdict and tuples
- d = defaultdict((int,float))
- for z in range( lots_and_lots):
- d['operation one'] += (1,5.67)
- ...
- ...
- d['operation two'] += (1,4.56)
- for k,v in d.items():
- print k, 'Called', v[0], 'times, total =', v[1]
- >>> x = (1,0)
- >>> x+= (2,3)
- >>> x
- (1, 0, 2, 3)
- >>> x = (1,0)
- >>> x+= (2,3)
- >>> x
- (3,3)
- d = defaultdict(list)
- for z in range(lots_and_lots):
- d['operation one'].append(5.67)
- ...
- ...
- d['operation two'].append(4.56)
- for k,v in d.items():
- print k, 'Called', len(v), 'times, total =', sum(v)
- class Inc(object):
- def __init__(self):
- self.i = 0
- self.t = 0.0
- def __iadd__(self, f):
- self.i += 1
- self.t += f
- return self
- d = defaultdict(Inc)
- for z in range(lots_and_lots):
- d['operation one'] += 5.67
- ...
- ...
- d['operation two'] += 4.56
- for k,v in d.items():
- print k, 'Called', v.i, 'times, total =', v.t
- >>> from collections import Counter, defaultdict
- >>> d = defaultdict(Counter)
- >>> d['operation_one'].update(ival=1, fval=5.67)
- >>> d['operation_two'].update(ival=1, fval=4.56)
- d = defaultdict(lambda: (0, 0.0))
- left, right = d["key"]
- d["key"] = (left + 2, right + 3)
- class vector(tuple):
- def __add__(self, other):
- return type(self)(l+r for l, r in zip(self, other))
- def __sub__(self, other):
- return type(self)(l-r for l, r in zip(self, other))
- def __radd__(self, other):
- return type(self)(l+r for l, r in zip(self, other))
- def __lsub__(self, other):
- return type(self)(r-l for l, r in zip(self, other))
- from collections import defaultdict
- d = defaultdict(lambda:vector((0, 0.0)))
- for k in range(5):
- for j in range(5):
- d[k] += (j, j+k)
- print d
- a = (1,0)
- b = (2,3)
- res = tuple(sum(x) for x in zip(a,b)
- d = defaultdict((int,float))
- for z in range( lots_and_lots):
- d['operation one'] = tuple(sum(x) for x in zip(d['operation one'], (1,5.67))
- ...
- ...
- class Tracker(object):
- def __init__(self):
- self.values = None
- self.count = 0
- def __iadd__(self, newvalues):
- self.count += 1
- if self.values is None:
- self.values = newvalues
- else:
- self.values = [(old + new) for old, new in zip(self.values, newvalues)]
- return self
- def __repr__(self):
- return '<Tracker(%s, %d)>' % (self.values, self.count)
- for k,v in d.items():
- print k, 'Called', v.count, 'times, total =', v.values