python中dtype,type,astype的区别

2021/5/2 20:28:44

本文主要是介绍python中dtype,type,astype的区别,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

python中dtype,type,astype的区别

  • type()
  • dtype()
  • astype()
函数名称用法
type 返回参数的数据类型
dtype 返回数组中元素的数据类型
astype 数据类型转换

type()

#type用于获取数据类型
import numpy as np
a=[1,2,3]
print(type(a))
#>>><class 'list'>
b=np.array(a)
print(type(b))
#>>><class 'numpy.ndarray'>

dtype()

#dtype用于获取数组中元素的类型
print(b.dtype)
#>>>int64

x,y,z=1,2,3
c=np.array([x,y,z])
print(c.dtype)
#>>>int64

d=np.array([1+2j,2+3j,3+4j])
print(d.dtype)
#>>>complex128

astype()

#astype修改数据类型
e=np.linspace(1,5,20)
print(e)
#>>>
'''
[1.         1.21052632 1.42105263 1.63157895 1.84210526 2.05263158
 2.26315789 2.47368421 2.68421053 2.89473684 3.10526316 3.31578947
 3.52631579 3.73684211 3.94736842 4.15789474 4.36842105 4.57894737
 4.78947368 5.        ]
'''
print(e.dtype)
#>>>float64

e=e.astype(int)
print(e)
#>>>[1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 5]
print(e.dtype)
#>>>int64

 

 



这篇关于python中dtype,type,astype的区别的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程