[英]Recasting NumPy array slice with astype
The code编码
a = 100. * np.random.randn(200)
a = a.astype(int)
recasts a
as an array of integer numbers.将a
重铸为 integer 数字的数组。 Meanwhile, the code同时,代码
a = 100. * np.random.randn(200)
a[:] = a.astype(int)
recasts a
as an array of integer floats obtained by truncating the original values of a
.将a
重铸为通过截断a
的原始值获得的 integer浮点数的数组。 Why doesn't the second case also give integers?为什么第二种情况也不给出整数?
I'm currently using Python 3.8 and NumPy 1.23.我目前正在使用 Python 3.8 和 NumPy 1.23。
To clarify, a.astype(int)
return a new array with dtype('int32')
.为了澄清, a.astype(int)
返回一个带有dtype('int32')
的新数组。
Line a = a.astype(int)
alters array a
with the new array return by astype(int)
.行a = a.astype(int)
用astype(int)
返回的新数组更改数组a
。
Meanwhile, a[:] = a.astype(int)
only update the values of the a
array, not the dtype.同时, a[:] = a.astype(int)
只更新a
数组的值,而不更新 dtype。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.