gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:30:35 29 4
gpt4 key购买 nike

将 numpy float 与列表相乘时, 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

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

最佳答案

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

关于python - 为什么在与列表相乘时将 float64 转换为 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27648480/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com