python 中的 type(), dtype(), astype()的区别
函数 | 说明 |
type() | 返回数据结构类型(list、dict、numpy.ndarray 等) |
dtype() |
返回数据元素的数据类型(int、float等) 备注:1)由于 list、dict 等可以包含不同的数据类型,因此不可调用dtype()函数 2)np.array 中要求所有元素属于同一数据类型,因此可调用dtype()函数 |
astype() |
改变np.array中所有数据元素的数据类型。 备注:能用dtype() 才能用 astype() |
测试代码:
import numpy as np
class Myclass():passa = [[1,2,3],[4,5,6]]
b = {'a':1,'b':2,'c':3}
c = np.array([1,2,3])
d = Myclass()
e = np.linspace(1,5,10)
c_ = c.astype(np.float)
f = 10print("type(a)=", type(a)) # type(a)= <class 'list'>
print("type(b)=", type(b)) # type(b)= <class 'dict'>
print("type(c)=", type(c)) # type(c)= <class 'numpy.ndarray'>
print("type(d)=", type(d)) # type(d)= <class '__main__.Myclass'>
print("type(e)=", type(e)) # type(e)= <class 'numpy.ndarray'>
print("type(f)=", type(f)) # type(f)= <class 'int'>
print("type(c_)=", type(c_)) # type(c_)= <class 'numpy.ndarray'># print(a.dtype) ## AttributeError: 'list' object has no attribute 'dtype'
# print(b.dtype) ## AttributeError: 'dict' object has no attribute 'dtype'
print(c.dtype) ## int32
# print(d.dtype) ## AttributeError: 'Myclass' object has no attribute 'dtype'
print(e.dtype) ## float64
print(c_.dtype) ## float64
# print(f.dtype) ## AttributeError: 'int' object has no attribute 'dtype'# print(a.astype(np.int)) ## AttributeError: 'list' object has no attribute 'astype'
# print(b.astype(np.int)) ## AttributeError: 'dict' object has no attribute 'astype'
print(c.astype(np.int)) ## [1 2 3]
# print(d.astype(np.int)) ## AttributeError: 'Myclass' object has no attribute 'astype'
print(e.astype(np.int)) ## [1 1 1 2 2 3 3 4 4 5]
# print(f.astype(np.int)) ## AttributeError: 'int' object has no attribute 'astype'
python 中的 type(), dtype(), astype()的区别相关推荐
- python中dtype什么意思_浅谈python 中的 type(), dtype(), astype()的区别
如下所示: 函数 说明 type() 返回数据结构类型(list.dict.numpy.ndarray 等) dtype() 返回数据元素的数据类型(int.float等) 备注:1)由于 list. ...
- python dtype什么意思_浅谈python 中的 type(), dtype(), astype()的区别
如下所示: 函数 说明 type() 返回数据结构类型(list.dict.numpy.ndarray 等) dtype() 返回数据元素的数据类型(int.float等) 备注:1)由于 list. ...
- python中astype用法_浅谈python 中的 type(), dtype(), astype()的区别
如下所示: 函数 说明 type() 返回数据结构类型(list.dict.numpy.ndarray 等) dtype() 返回数据元素的数据类型(int.float等) 备注:1)由于 list. ...
- Python中辨析type/dtype/astype用法
Python中与数据类型相关函数及属性有如下三个:type/dtype/astype. 名称 描述 type() 返回参数的数据类型 dtype 返回数组中元素的数据类型 astyp ...
- python中type(),dtype(),astype()的区别
python中type(),dtype(),astype()的区别 type(): 返回的是数据结构的类型(list, dict,numpy.ndarry) >>> k = [1, ...
- python中numpy的dtype
type,dtype,astype的区别 type():返回数据结构的类型(list,dict等).如type([1,2,3,4])返回list.list中可以包含不同数据类型的元素. dtype() ...
- Python中type/dtype/astype用法
Python中与数据类型相关函数及属性有如下三个:type/dtype/astype. 名称 描述 type() 返回参数的数据类型 dtype 返回数组中元素的数据类型 astype() 对数据类型 ...
- Python-辨析type/dtype/astype用法
Python中与数据类型相关函数及属性有如下三个:type/dtype/astype. 名称 描述 type() 返回参数的数据类型 dtype 返回数组中元素的数据类型 astype() 对数据类型 ...
- Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用...
Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象 ...
最新文章
- svn cleanup failed–previous operation has not finished 解决方法
- Single Number
- [Python 多线程] Semaphore、BounedeSemaphore (十二)
- 10个常见的Redis面试刁难问题--转
- CSS左中右布局,规范案例
- 微信小程序入门二: 条件、遍历、网络请求、获取本地图片
- 新议题,好多年前的华为面试题
- php微型mvc框架创建步骤
- STM32固件库详解
- 【Unity3D插件】MiniMap插件分享《小地图插件》
- 用JS实现视频播放器
- 在动态表格中智能搜索框JS实现(layui的智能搜索不能手动输入的问题解决)
- 投我以木桃,报之以琼浆。匪报也,永以为好也!
- 实战演练-抽奖程序(C#)(第一部分)
- 安徽信息技术初中会考上机考试模拟_初中会考信息技术考试模拟试题一
- pwn-栈迁移-ROP
- Learning Synergies between Pushing and Grasping with Self-supervised Deep Reinforcement Learning
- 笔记本计算机无法开机密码,笔记本开机密码搞忘了如何解决_笔记本电脑忘了开机密码怎么办-win7之家...
- 公钥 私钥 数字签名 CA证书
- 高清录屏去噪方法?--QVE屏幕录制
热门文章
- givemesomecredit数据_EasyEnsemble:一种简单的不平衡数据的建模方法(附测试代码)...
- 圣经 英文有声 android,英文有声+Epub Foundation Series 7部 Isaac Asimov - Audiobook
- 计算机打开共享网络连接打印机共享打印机,WIN7怎么连接共享打印机?如何设置和连接局域网共享打印机...
- python写日志到文件_python 通过logging写入日志到文件和控制台的实例
- win8计算机配置怎么看,win8怎么看电脑配置?win8电脑配置的查看方法
- primefaces教程_Primefaces BlockUI组件示例教程
- JUnit测试用例– Eclipse和Maven
- angularjs绑定属性_AngularJS隔离范围绑定表达式教程
- 学习C++开发技术,常见误解有哪些?
- C++中的cin cout