小编wer*_*ere的帖子

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

将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]
Run Code Online (Sandbox Code Playgroud)

普通的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'
Run Code Online (Sandbox Code Playgroud)

但是,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 …
Run Code Online (Sandbox Code Playgroud)

python numpy

9
推荐指数
1
解决办法
554
查看次数

标签 统计

numpy ×1

python ×1