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

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 0.64 KB  |  hits: 9  |  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. Map-summing items in a multidimensional array
  2. [ (0.5, 0.6, 0.7), (0.1, 0.9, 0.8), (0.9, 1.0, 0.4),
  3.   ...
  4.   (0.3, 0.8, 0.3), (0.2, 0.4, 0.9), (0.5, 0.5, 0.3) ]
  5.        
  6. >>> c=[ (0.5, 0.6, 0.7), (0.1, 0.9, 0.8), (0.9, 1.0, 0.4),(0.5, 0.6, 0.7), (0.1, 0.9, 0.8), (0.9, 1.0, 0.4),(0.5, 0.6, 0.7), (0.1, 0.9, 0.8), (0.9, 1.0, 0.4)]
  7. >>> [ sum([t[i] for t in c]) for i in range(len(c[0]))]
  8. [4.5, 7.5, 5.6999999999999993]
  9.        
  10. result = reduce(lambda x, y: tuple((xi + yi) for (xi, yi) in zip (x, y)), l)
  11.        
  12. >>> reduce(lambda x, y: (x[0]+y[0], x[1]+y[1], x[2]+y[2]), [(1, 2, 3), (2, 3, 4), (4, 5, 6)])
  13. (7, 10, 13)
  14.        
  15. values = [(1,2,3), (4,5,6)]
  16. print map(sum, values)