How Do I Combine Indexes Of Two Lists?
Let's say I have the lists [[1,2],[3,4]] and [[5,6],[7,8]] I expect [[6, 8], [10, 12]] as the result. I'm trying to sum up numbers according to their indexes. def
Solution 1:
Another easy job for ndarrays:
>>> from numpy import array
>>> list1, list2 = [[1,2],[3,4]], [[5,6],[7,8]]
>>> (array(list1) + array(list2)).tolist()
[[6, 8], [10, 12]]
Solution 2:
onliner trivial recursive lambda:
sum_mtx = lambda (x, y): x+y ifnot isinstance(x, list) else map(sum_mtx, zip(x,y))
sum_mtx(([[1, 2], [3, 4]] ,[[5, 6], [7, 8]]))
# [[6, 8], [10, 12]]
Post a Comment for "How Do I Combine Indexes Of Two Lists?"