问题描述
x = np.arange(0.3,12.5,0.6)
x = np.arange(0.3, 12.5, 0.6)
print(x)
[0.3 0.9 1.5 2.1 2.7 3.3 3.9 4.5 5.1 5.7 6.3 6.9 7.5 8.7 8.7 9.3 9.9 10.5 11.1 11.7 12.3]
[ 0.3 0.9 1.5 2.1 2.7 3.3 3.9 4.5 5.1 5.7 6.3 6.9 7.5 8.1 8.7 9.3 9.9 10.5 11.1 11.7 12.3]
x = np.arange(0.3,12.5,0.6,int)
x = np.arange(0.3, 12.5, 0.6,int)
print(x)
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
推荐答案
首先,让我们跳过浮点步骤的复杂性,并使用简单的整数开始和结束:
First let's skip the complexity of a float step, and use a simple integer start and stop:
In [141]: np.arange(0,5)
Out[141]: array([0, 1, 2, 3, 4])
In [142]: np.arange(0,5, dtype=int)
Out[142]: array([0, 1, 2, 3, 4])
In [143]: np.arange(0,5, dtype=float)
Out[143]: array([0., 1., 2., 3., 4.])
In [144]: np.arange(0,5, dtype=complex)
Out[144]: array([0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j])
In [145]: np.arange(0,5, dtype='datetime64[D]')
Out[145]:
array(['1970-01-01', '1970-01-02', '1970-01-03', '1970-01-04',
'1970-01-05'], dtype='datetime64[D]')
即使在一定范围内,bool
也可以正常工作:
Even bool
work, within a certain range:
In [149]: np.arange(0,1, dtype=bool)
Out[149]: array([False])
In [150]: np.arange(0,2, dtype=bool)
Out[150]: array([False, True])
In [151]: np.arange(0,3, dtype=bool)
ValueError: no fill-function for data-type.
In [156]: np.arange(0,3).astype(bool)
Out[156]: array([False, True, True])
有2个可能的布尔值,因此要求更多布尔值会产生某种错误.
There are 2 possible boolean values, so asking for more should produce some sort of error.
arange
是已编译的代码,因此我们无法随时检查其逻辑(但欢迎您在github上搜索C代码).
arange
is compiled code, so we can't readily examine its logic (but you are welcome to search for the C code on github).
示例表明,从某种意义上讲,它确实将参数转换为相应的dtype
,并对此进行了迭代.它不只是生成范围并最终转换为dtype.
The examples show that it does, in some sense convert the parameters into the corresponding dtype
, and perform an iteration on that. It doesn't simply generate the range and convert to dtype at the end.
这篇关于python numpy arange dtpye?为什么转换为整数为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!