这是场景:给定n 个整数列表(长度相同)和一个累加器(实际上长度相同),就地累加元素总和。就地约束在这里,因为我在列表的字典中累积值(嗯......不太清楚,请参见下面的示例)
编辑:我正在寻找不涉及 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
虽然它有效并且我可以专业地接受它,但我个人不能。
谁能让我今晚睡得更好?