I am trying to multiply two arrays by using '@' but this is the error I get
a@b
unsupported operand type(s) for @: 'list' and 'list'
I read that '@' is used for matrix multiplication, which is what I need
I am trying to multiply two arrays by using '@' but this is the error I get
a@b
unsupported operand type(s) for @: 'list' and 'list'
I read that '@' is used for matrix multiplication, which is what I need
From the error message it appears as though you are using lists
. You need to use arrays
for which you can use the numpy
module like this:
import numpy as np
x = [1,2,3,4]
y = [10,20,30,40]
x = np.array(x)
y = np.array(y)
z = x@y
print(z)
which returns this:
300
If you want to multiply lists without numpy
you can do this:
# method 1
z = 0
for i, v in enumerate(x):
z = z + (x[i]*y[i])
print(z)
# method 2
z = sum([x*y for x,y in zip(x,y)])
print(z)
Both would return the same value as above...