Here is the scenario: given n lists (of identical length) of integers and an accumulator (of the same length, indeed), accumulate the element-wise sum, in-place. The in-place constraint is here because I accumulate values in a dict of lists (hum ... quite not clear, see the example below)
EDIT: I'm looking for a solution that does not involve numpy
# My lists are long (they are actually pixels in 1000x1000 images)
# but I keep l low for the sake of the example
l = 5
# Values here are arbitrary and won't be repeated in the real word
# e.g. list 1 might be [41,15,0,2,3], etc.
lists = [
{'id': 1, 'values': [12]*l},
{'id': 2, 'values': [42]*l},
{'id': 2, 'values': [25]*l},
{'id': 1, 'values': [6]*l},
]
maps = {
1: [0]*l,
2: [0]*l
}
for item in lists:
# Get the "target" for this list
target = maps[item['id']]
# Element-wise addition of item['values'] to target here!
# This won't work
target = map(lambda x,y:x+y, target, item['values'])
# This neither
target = [(x+y) for x,y in itertools.izip(target,item['values'])]
# For either of the previous to work, I need to re-assign
# the result to 'target', like so
maps[item['id']] = target
While it works and I can professionally live with it, I personally can't.
Can anyone make me sleep better tonight ?