python 3.x - Summing two 2-D lists -


so has kind of stumped me. feel should easy problem though. lets have these 2 lists

a = [[3, 4], [4, 5]] b = [[1, 2], [4, 6]] 

i trying return sum of 2 2-d lists of each corresponding element

c = [[4, 6], [8, 11]] 

i pretty sure getting lost in loops. trying use nested loops produce answer, suggestions? i'm trying several different things code not complete or set in stone , change time reponds won't leave code here. trying though!

you try variation on nested for-loops using enumerate (which give appropriate indices comparison other 2d array):

a = [[3, 4], [4, 5]] b = [[1, 2], [4, 6]] 

edit: didn't see wanted populate new list, put in there:

>>> c = [] >>> val, item in enumerate(a):         newvals = []         itemval, insideitem in enumerate(item):            newvals.append(insideitem + b[val][itemval])          c.append(newvals)          newvals = [] 

result:

>>> c [[4, 6], [8, 11]] 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -