Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 15th, 2012  |  syntax: None  |  size: 2.42 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. defaultdict and tuples
  2. d = defaultdict((int,float))
  3. for z in range( lots_and_lots):
  4.   d['operation one'] += (1,5.67)
  5.   ...
  6.   ...
  7.   d['operation two'] += (1,4.56)
  8.        
  9. for k,v in d.items():
  10.   print k, 'Called', v[0], 'times, total =', v[1]
  11.        
  12. >>> x = (1,0)
  13. >>> x+= (2,3)
  14. >>> x
  15. (1, 0, 2, 3)
  16.        
  17. >>> x = (1,0)
  18. >>> x+= (2,3)
  19. >>> x
  20. (3,3)
  21.        
  22. d = defaultdict(list)
  23. for z in range(lots_and_lots):
  24.   d['operation one'].append(5.67)
  25.   ...
  26.   ...
  27.   d['operation two'].append(4.56)
  28. for k,v in d.items():
  29.   print k, 'Called', len(v), 'times, total =', sum(v)
  30.        
  31. class Inc(object):
  32.     def __init__(self):
  33.         self.i = 0
  34.         self.t = 0.0
  35.     def __iadd__(self, f):
  36.         self.i += 1
  37.         self.t += f
  38.         return self
  39.        
  40. d = defaultdict(Inc)
  41. for z in range(lots_and_lots):
  42.   d['operation one'] += 5.67
  43.   ...
  44.   ...
  45.   d['operation two'] += 4.56
  46. for k,v in d.items():
  47.   print k, 'Called', v.i, 'times, total =', v.t
  48.        
  49. >>> from collections import Counter, defaultdict
  50. >>> d = defaultdict(Counter)
  51. >>> d['operation_one'].update(ival=1, fval=5.67)
  52. >>> d['operation_two'].update(ival=1, fval=4.56)
  53.        
  54. d = defaultdict(lambda: (0, 0.0))
  55.        
  56. left, right = d["key"]
  57. d["key"] = (left + 2, right + 3)
  58.        
  59. class vector(tuple):
  60.     def __add__(self, other):
  61.         return type(self)(l+r for l, r in zip(self, other))
  62.     def __sub__(self, other):
  63.         return type(self)(l-r for l, r in zip(self, other))
  64.     def __radd__(self, other):
  65.         return type(self)(l+r for l, r in zip(self, other))
  66.     def __lsub__(self, other):
  67.         return type(self)(r-l for l, r in zip(self, other))
  68.  
  69. from collections import defaultdict
  70.  
  71. d = defaultdict(lambda:vector((0, 0.0)))
  72. for k in range(5):
  73.     for j in range(5):
  74.         d[k] += (j, j+k)
  75.  
  76. print d
  77.        
  78. a = (1,0)
  79. b = (2,3)
  80.  
  81. res = tuple(sum(x) for x in zip(a,b)
  82.        
  83. d = defaultdict((int,float))
  84. for z in range( lots_and_lots):
  85.   d['operation one'] = tuple(sum(x) for x in zip(d['operation one'], (1,5.67))
  86.   ...
  87.   ...
  88.        
  89. class Tracker(object):
  90.     def __init__(self):
  91.         self.values = None
  92.         self.count = 0
  93.  
  94.     def __iadd__(self, newvalues):
  95.         self.count += 1
  96.         if self.values is None:
  97.             self.values = newvalues
  98.         else:
  99.             self.values = [(old + new) for old, new in zip(self.values, newvalues)]
  100.         return self
  101.  
  102.     def __repr__(self):
  103.         return '<Tracker(%s, %d)>' % (self.values, self.count)
  104.        
  105. for k,v in d.items():
  106.     print k, 'Called', v.count, 'times, total =', v.values