Reputation: 60
I have a basic question:
There are two lists named a1
and b1
.
If I print out one item of each list, it would be a float number
, but when I use a1[i]*b1[i]
in a loop, it gives an error:
TypeError: can't multiply sequence by non-int of type 'float'
Why is that?
Upvotes: 1
Views: 136
Reputation: 384
Think @Gille probably got your error.
If all you want to do in your loop is multiply entries together, the fast way would be to use numpy arrays:
import numpy as np
result = np.multiply(a1,b1)
Cast back to a list if necessary:
result = list(result)
Upvotes: 1
Reputation: 5773
Either a1
or b1
is not list of floats but a list of lists of floats.
a1=[1.234, 1.234];
a2=[1.234, 1.234];
>>> a1[0]*a2[0]
1.522756
a3=[[1.234], [1.234]];
>>> a1[0]*a3[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'
Upvotes: 3