[Numpy-discussion] Multiplying numpy floats and python lists

Warren Weckesser warren.weckesser at enthought.com
Sun Jun 20 14:12:42 EDT 2010


Tony S Yu wrote:
> I came across some strange behavior when multiplying numpy floats and python lists: the list is returned unchanged:
>
>   
>> In [18]: np.float64(1.2) * [1, 2]
>>
>> Out[18]: [1, 2]
>>     
>
>   

Apparently the np.float64 object is cast to an int, and python's * is used:

In [7]: np.float64(2.5) * [1,2]
Out[7]: [1, 2, 1, 2]

In [8]: np.float64(3.8) * [1,2]
Out[8]: [1, 2, 1, 2, 1, 2]


Warren


> On the other hand, multiplying an array scalar and a python list gives the expected answer
>
>   
>> In [19]: np.array(1.2) * [1, 2]
>>
>> Out[19]: array([ 1.2,  2.4])
>>     
>
> Finally, multiplying a python float and a python list gives an error:
>
>   
>> In [20]: 1.2 * [1, 2]
>> ---------------------------------------------------------------------------
>> TypeError                                 Traceback (most recent call last)
>>
>> /Users/Tony/<ipython console> in <module>()
>>
>> TypeError: can't multiply sequence by non-int of type 'float'
>>     
>
> These last two results (with the array scalar and the python float) both seem appropriate, but the numpy-float behavior is surprising. Is this a bug?
>
> -Tony
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>   




More information about the NumPy-Discussion mailing list