gpt4 book ai didi

Python 'astype' 不工作

转载 作者:太空狗 更新时间:2023-10-30 01:51:15 24 4
gpt4 key购买 nike

我目前正在使用 Anaconda 的 Spyder,我正在尝试将包含 float 类型的数组转换为 int 类型:

x = np.array([1, 2, 2.5])
x.astype(int)
print x

结果还是出来了:

[1. 2. 2.5]

想法?

最佳答案

astype returns a new array .您需要将结果分配给 x:

In [298]: x = x.astype(int)

In [299]: x
Out[299]: array([1, 2, 2])

关于Python 'astype' 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23143850/

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