Element-wise product of two 2-D lists

前端 未结 4 1731
[愿得一人]
[愿得一人] 2021-01-21 05:06

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 dimensi

4条回答
  •  情话喂你
    2021-01-21 05:39

    You could use numpy arrays. They are your best option as they run on a C background and hence are much faster computationally

    First, install numpy. Shoot up your terminal (CMD if you're in windows), type

    pip install numpy
    

    or, if in Linux, sudo pip install numpy

    Then, go on to write your code

    import numpy as np
    
    list1=np.array([[2,3,5,6,7],[5,2,9,3,7]]) #2 dimensional list example
    list2=np.array([[5,2,9,3,7],[1,3,5,2,2]])
    
    prod = np.multiply(list1,list2)
    # or simply, as suggested by Mad Physicist,
    prod = list1*list2
    

提交回复
热议问题