The flat property gives back a numpy.flatiter object.This is the only means to get a flatiter object;we do not have access to a flatiter constructor.The flat iterator enables us to loop through an array as if it were a flat array,as shown in the following code object:
In [80]: b
Out[80]:
array([[0, 1],
[2, 3]])
In [81]: f = b.flat
In [82]: f
Out[82]: <numpy.flatiter at 0x23ab790>
In [83]: for item in f: print(item)
0
1
2
3
In [84]: for item in f: print(item) #暂时还不了解这个为什么没有输出
In [85]: f = b.flat
In [86]: f
Out[86]: <numpy.flatiter at 0x23afac0>
In [87]: for item in f:
...: print(item)
...:
0
1
2
3
We can convert a NumPy array to a Python list with the tolist() function:
In [102]: b
Out[102]: array([ 1.+1.j, 3.+2.j])
In [103]: b.astype(int)
/home/maoxianxin/virtualenv/python3/dataAnalysis/bin/ipython:1: ComplexWarning: Casting complex values to real discards the imaginary part
#!/home/maoxianxin/virtualenv/python3/dataAnalysis/bin/python3
Out[103]: array([1, 3])
In [104]: b.astype(complex)
Out[104]: array([ 1.+1.j, 3.+2.j])
In [105]: b.astype('complex')
Out[105]: array([ 1.+1.j, 3.+2.j])
In [106]: b.astype('int')
/home/maoxianxin/virtualenv/python3/dataAnalysis/bin/ipython:1: ComplexWarning: Casting complex values to real discards the imaginary part
#!/home/maoxianxin/virtualenv/python3/dataAnalysis/bin/python3
Out[106]: array([1, 3])
We are dropping off the imaginary part when casting from the complex type to int.The astype() function takes the name of a data type as a string too.
numpy 学习5
最新推荐文章于 2022-06-20 15:34:20 发布