为什么float64在与列表相乘时转换为int?

时间:2014-12-25 15:21:42

标签: python numpy

当将numpy浮点数与列表相乘时,float会自动转换为int

>>> import numpy as np
>>> a = [1, 2, 3]
>>> np.float64(2.0) * a ### This behaves as 2 * a
[1, 2, 3, 1, 2, 3]

普通的float给出了TypeError

>>> 2.0 * a ### This does not
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'

但是,numpy float不能用于索引

>>> a[np.float64(2.0)]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not numpy.float64

这种行为背后的逻辑是什么?

1 个答案:

答案 0 :(得分:2)

你在NumPy中遇到了known bug。 GitHub问题于去年关闭,但行为仍然存在于NumPy 1.9.1版本中。