两个二维列表的按元素乘积 [英] Element-wise product of two 2-D lists
问题描述
我不能使用Numpy或任何其他库函数,因为这是我必须要做的问题,我必须定义自己的方式.
I can't use Numpy or any other library function as this is a question I have to do, I have to define my own way.
我正在编写一个以两个列表(二维)为参数的函数.该函数应计算两个列表的元素乘积,并将它们存储在第三个列表中,然后从函数返回此结果列表. 输入列表的示例为:
I am writing a function that takes two lists (2 dimensional) as arguments. The function should calculate the element-wise product of both lists and store them in a third list and return this resultant list from the function. An example of the input lists are:
[[2,3,5,6,7],[5,2,9,3,7]]
list2:
[[5,2,9,3,7],[1,3,5,2,2]]
该函数将打印以下列表:
The function prints the following list:
[[10, 6, 45, 18, 49], [5, 6, 45, 6, 14]]
即2*5=10
,3*2=6
,5*9=45
...等.
这是我的下面代码,但仅适用于其中包含2个列表(元素)的列表,就像上面的示例一样,并且效果很好,但是我想要编辑我的代码,以便无论如何2-D列表中有许多列表(元素),它应该在新的2-D列表中打印出其按元素的乘积,例如它也应该适用于
This is my code below, but it is only for a list with 2 lists (elements) inside in it like the example above and works perfectly fine for that, but what I want is to edit my code so that no matter how many number of lists (elements) are there in the 2-D list, it should print out its element-wise product in a new 2-D list e.g. it should also work for
[[5,2,9,3,7],[1,3,5,2,2],[1,3,5,2,2]]
或
[[5,2,9,3,7],[1,3,5,2,2],[1,3,5,2,2],[5,2,9,3,7]]
或整个列表中有任意数量的列表.
or any number of lists there are within the whole list.
def ElementwiseProduct(l,l2):
i=0
newlist=[] #create empty list to put prouct of elements in later
newlist2=[]
newlist3=[] #empty list to put both new lists which will have proudcts in them
while i==0:
a=0
while a<len(l[i]):
prod=l[i][a]*l2[i][a] #corresponding product of lists elements
newlist.append(prod) #adding the products to new list
a+=1
i+=1
while i==1:
a=0
while a<len(l[i]):
prod=l[i][a]*l2[i][a] #corresponding product of lists elements
newlist2.append(prod) #adding the products to new list
a+=1
i+=1
newlist3.append(newlist)
newlist3.append(newlist2)
print newlist3
#2 dimensional list example
list1=[[2,3,5,6,7],[5,2,9,3,7]]
list2=[[5,2,9,3,7],[1,3,5,2,2]]
ElementwiseProduct(list1,list2)
推荐答案
您可以 zip
列表理解中的两个列表,然后进一步 zip
生成的子列表,然后最后将各项相乘:
You can zip
the two lists in a list comprehension, then further zip
the resulting sublists and then finally multiply the items:
list2 = [[5,2,9,3,7],[1,3,5,2,2]]
list1 = [[2,3,5,6,7],[5,2,9,3,7]]
result = [[a*b for a, b in zip(i, j)] for i, j in zip(list1, list2)]
print(result)
# [[10, 6, 45, 18, 49], [5, 6, 45, 6, 14]]
Should in case the lists/sublists do not have the same number of elements, itertools.izip_longest
can be used to generate fill values such as an empty sublist for the smaller list, or 0 for the shorter sublist:
from itertools import izip_longest
list1 = [[2,3,5,6]]
list2 = [[5,2,9,3,7],[1,3,5,2,2]]
result = [[a*b for a, b in izip_longest(i, j, fillvalue=0)]
for i, j in izip_longest(list1, list2, fillvalue=[])]
print(result)
# [[10, 6, 45, 18, 0], [0, 0, 0, 0, 0]]
您可以将内部fillvalue
从0更改为1,以按原样返回较长子列表中的元素,而不是同质0.
You may change the inner fillvalue
from 0 to 1 to return the elements in the longer sublists as is, instead of a homogeneous 0.
参考:
这篇关于两个二维列表的按元素乘积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!