将二维列表乘以一维列表到每个元素
Multiplying 2D list by 1D list to each elements
我想将一维列表与二维列表的元素相乘。我怎样才能用列表理解来做到这一点?
a = [1,2]
b = [[3,4],[5,6]]
想要的结果是
c = [[3,8],[5,12]
我试过这种方法
c = [i*j for i, j in zip(a,b)]
我遇到的错误是
TypeError: can't multiply sequence by non-int of type 'list'
您可以使用嵌套列表理解:
c = [ [x * y for x, y in zip(a, row)] for row in b ]
我想将一维列表与二维列表的元素相乘。我怎样才能用列表理解来做到这一点?
a = [1,2]
b = [[3,4],[5,6]]
想要的结果是
c = [[3,8],[5,12]
我试过这种方法
c = [i*j for i, j in zip(a,b)]
我遇到的错误是
TypeError: can't multiply sequence by non-int of type 'list'
您可以使用嵌套列表理解:
c = [ [x * y for x, y in zip(a, row)] for row in b ]