Use map with operator.add:
>>> from operator import add
>>> list( map(add, list1, list2) )
[5, 7, 9]
or zip with a list comprehension:
>>> [sum(x) for x in zip(list1, list2)]
[5, 7, 9]
Timing comparisons:
>>> list2 = [4, 5, 6]*10**5
>>> list1 = [1, 2, 3]*10**5
>>> %timeit from operator import add;map(add, list1, list2)
10 loops, best of 3: 44.6 ms per loop
>>> %timeit from itertools import izip; [a + b for a, b in izip(list1, list2)]
10 loops, best of 3: 71 ms per loop
>>> %timeit [a + b for a, b in zip(list1, list2)]
10 loops, best of 3: 112 ms per loop
>>> %timeit from itertools import izip;[sum(x) for x in izip(list1, list2)]
1 loops, best of 3: 139 ms per loop
>>> %timeit [sum(x) for x in zip(list1, list2)]
1 loops, best of 3: 177 ms per loop
Answer from Ashwini Chaudhary on Stack OverflowMy data is structured in a Python dictionary as:
dict = {
'key1': {
'data1': [10, 20, 30, 40, 50]
},
'key2': {
'data2': [60, 70, 80, 90, 100]
},
'key3': {
'data3': [110, 120, 130, 140, 150]
}
}where the three lists have the same number of elements.
Now, I'd like to sum, in a element-wise manner, the three "dataX" lists, in order to obtain a single list.
In this example, there are three keys (and, then, three lists), but they could also be one, two, or four.
Which is a good way to achieve this goal?
python - Find the sum of two lists of lists element-wise - Stack Overflow
sum of N lists element-wise python - Stack Overflow
python - Element-wise sum of nested lists - Stack Overflow
Sum element by element list of lists python - Stack Overflow
Use map with operator.add:
>>> from operator import add
>>> list( map(add, list1, list2) )
[5, 7, 9]
or zip with a list comprehension:
>>> [sum(x) for x in zip(list1, list2)]
[5, 7, 9]
Timing comparisons:
>>> list2 = [4, 5, 6]*10**5
>>> list1 = [1, 2, 3]*10**5
>>> %timeit from operator import add;map(add, list1, list2)
10 loops, best of 3: 44.6 ms per loop
>>> %timeit from itertools import izip; [a + b for a, b in izip(list1, list2)]
10 loops, best of 3: 71 ms per loop
>>> %timeit [a + b for a, b in zip(list1, list2)]
10 loops, best of 3: 112 ms per loop
>>> %timeit from itertools import izip;[sum(x) for x in izip(list1, list2)]
1 loops, best of 3: 139 ms per loop
>>> %timeit [sum(x) for x in zip(list1, list2)]
1 loops, best of 3: 177 ms per loop
The others gave examples how to do this in pure python. If you want to do this with arrays with 100.000 elements, you should use numpy:
In [1]: import numpy as np
In [2]: vector1 = np.array([1, 2, 3])
In [3]: vector2 = np.array([4, 5, 6])
Doing the element-wise addition is now as trivial as
In [4]: sum_vector = vector1 + vector2
In [5]: print sum_vector
[5 7 9]
just like in Matlab.
Timing to compare with Ashwini's fastest version:
In [16]: from operator import add
In [17]: n = 10**5
In [18]: vector2 = np.tile([4,5,6], n)
In [19]: vector1 = np.tile([1,2,3], n)
In [20]: list1 = [1,2,3]*n
In [21]: list2 = [4,5,6]*n
In [22]: timeit map(add, list1, list2)
10 loops, best of 3: 26.9 ms per loop
In [23]: timeit vector1 + vector2
1000 loops, best of 3: 1.06 ms per loop
So this is a factor 25 faster! But use what suits your situation. For a simple program, you probably don't want to install numpy, so use standard python (and I find Henry's version the most Pythonic one). If you are into serious number crunching, let numpy do the heavy lifting. For the speed freaks: it seems that the numpy solution is faster starting around n = 8.
You could just use zip like,
>>> list1
[[1, 2, 3], [4, 5, 6]]
>>> list2
[[10, 2, 3], [11, 5, 6]]
>>> [[x+y for x,y in zip(l1, l2)] for l1,l2 in zip(list1,list2)]
[[11, 4, 6], [15, 10, 12]]
or if you are not sure, if both list will be of same length, then you can use zip_longest (izip_longest in python2) from itertools and use the fillvalue like,
>>> import itertools
>>> y = itertools.zip_longest([1,2], [3,4,5], fillvalue=0)
>>> list(y)
[(1, 3), (2, 4), (0, 5)]
that then you can use it for unequal sized data like,
>>> from itertools import zip_longest
>>> list1=[[1, 2, 3], [4, 5]]
>>> list2=[[10, 2, 3], [11, 5, 6], [1,2,3]]
>>> [[x+y for x,y in zip_longest(l1, l2, fillvalue=0)] for l1,l2 in zip_longest(list1,list2, fillvalue=[])]
[[11, 4, 6], [15, 10, 6], [1, 2, 3]]
In Python, it's rarely necessary to use indices, particularly for a task like this where you want to do the same thing to each element individually. The main function for that sort of transformation is map, with list comprehensions offering a convenient shorthand. However, map does one thing those don't - process multiple iterables in parallel, like Haskell's zipWith. We can break this down in two stages to do that:
list1 = [[1, 2, 3], [4, 5, 6]]
list2 = [[10, 2, 3], [11, 5, 6]]
from operator import add
def addvectors(a, b):
return list(map(add, a, b))
list3 = list(map(addvectors, list1, list2))
In Python 2, map returns a list, so you don't need to separately collect it as I've done with list here.
Just do this:
[sum(x) for x in zip(*C)]
In the above, C is the list of c_1...c_n. As explained in the link in the comments (thanks, @kevinsa5!):
*is the "splat" operator: It takes a list as input, and expands it into actual positional arguments in the function call.
For additional details, take a look at the documentation, under "unpacking argument lists" and also read about calls (thanks, @abarnert!)
This isn't all that different from Óscar López's answer, but uses itertools.imap instead of a list comprehension.
from itertools import imap
list(imap(sum, zip(*C))
Are you using Numpy?
If you are, I think you simply want np.sum(weighted_prob, axis=0)
sum_per_bin = []
This statement in the nested for assign a new list to the variable every time. So your dummy will only be the last item.
To get what you want:
[sum(x) for x in zip(*weighted_prob)]
You can try this:
L1 = [[1,2,3], [4,5,6], [7,8,9]]
L2 = [[10,20,30], [40,50,60], [70,80,90]]
final_list = [[c+d for c, d in zip(a, b)] for a, b in zip(L1, L2)]
Output:
[[11, 22, 33], [44, 55, 66], [77, 88, 99]]
Using zip and map with list comprehension :
>>> temp = zip(L1, L2)
# >>> list(temp)
# => [([1, 2, 3], [10, 20, 30]), ([4, 5, 6], [40, 50, 60]), ([7, 8, 9], [70, 80, 90])]
>>> [list(map(sum, zip(x,y))) for x,y in temp]
=> [[11, 22, 33], [44, 55, 66], [77, 88, 99]]
NOTE : using just zip(L1, L2) is better than doing list(zip(L1, L2)) as the former returns a Generator which is more efficient and faster than building a list.
Yes, you can 'add up' the sub lists to form new lists in d:
a = [[1,2,3], [4,5,6], [7,8,9]]
b = [[11,12,13], [14,15,16], [17,18,19]]
c = [[21,22,23], [24,25,26], [27,28,29]]
d = []
for a_i,b_i,c_i in zip(a,b,c):
d.append(a_i + b_i + c_i)
print(d)
Output as requested.
In fact, you can use the built-in sum():
d = []
for items in zip(a, b, c):
d.append(sum(items, start=[]))
print(d)
Try numpy:
import numpy as np
a = [[1,2,3],[4,5,6],[7,8,9]]
b = [[11,12,13],[14,15,16],[17,18,19]]
c = [[21,22,23],[24,25,26],[27,28,29]]
a = np.array(a)
b = np.array(b)
c = np.array(c)
d = np.concatenate([a, b, c], axis=1)
print(d)
#[[ 1 2 3 11 12 13 21 22 23]
#[ 4 5 6 14 15 16 24 25 26]
#[ 7 8 9 17 18 19 27 28 29]]