How Can Sum Two Nested List In This Situation
Given list a, b a=[[[1.1,-2.1], [-0.6,4.2]], [[3.9,1.3], [-1.3,1.2]]] b=[[-1.1,4.3], [-1.4,2.4]] If I just want to sum the list [[1.1,-2.1],[-0.6,4.2]] in the list
Solution 1:
Use:
import numpy as npc= (np.array(a[0]) + b[0]).tolist()
Output:
>>> c
[[0.0, 2.1999999999999997], [-1.7000000000000002, 8.5]]
Update Using for loop:
c = []
for row in a[0]:
c.append([])
for x, y in zip(row, b[0]):
c[-1].append(x+y)
Update: Using list comprehension
c = [[x + y for x,y in zip(row, b[0])] for row in a[0]]
Output:
>>> c
[[0.0, 2.1999999999999997], [-1.7000000000000002, 8.5]]
Post a Comment for "How Can Sum Two Nested List In This Situation"