將兩個list對應值相乘後得到的list值再相加

from functools import reduce
import torch


a = torch.tensor([1, 4])
b = torch.tensor([4, 7])
list1 = [a, b]

c = torch.tensor([7, 8])
d = torch.tensor([6, 12])
list2 = [c, d]

output = reduce(lambda x, y: x + y, map(lambda z: z[0] * z[1], zip(list1, list2)))
print(output)

out: tensor([ 31, 116])

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章