python 中的 type(), dtype(), astype()的区别

python 中的 type(), dtype(), astype()的区别函数 说明 type() 返回数据结构类型(list、dict、numpy.ndarray等) dtype() 返回数据元素的数据类型(int、float等) 备注:1)由于list、dict等可以包含不同的数据类型,因此不可调用dtype()函数 2)np.array中要求所有元素属于同一数据类型,因此可调用d…

大家好,又见面了,我是你们的朋友全栈君。

 

函数 说明
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():
    pass

a = [[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 = 10

print("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'

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/134155.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Java基础篇:Iterator迭代器

    Java基础篇:Iterator迭代器

    2021年10月4日
    44
  • QXDM打印高通sensor 日志问题总结

    QXDM打印高通sensor 日志问题总结在使用QXDM打印高通sensor日志的时候,经常会发现有些赋予已经权限很高的log居然打印不出来,这就个代码的追踪带来了一系列困难,鉴于此,我研究了一下高通中log打印问题,给大家今后的使用带来一些经验。在高通的关于日志的头文件定义中,许多日志是默认不打开的,研究代码:#if(BUILD_DRAGON_BOARD)&&(DEBUG_DATA)#defineLSM6DSM_DATA_M

    2022年9月29日
    3
  • [译文]三重缓冲:为什么我们爱它

    [译文]三重缓冲:为什么我们爱它
    文章来源:http://www.anandtech.com/video/showdoc.aspx?i=3591&p=1
    文章标题:TripleBuffering:WhyWeLoveIt
    文章作者:DerekWilson
    文章时间:2009年6月26日

    引子

    我们往往不愿过多讨论哪些选项在游戏中应该开启。相反,我们往往把重点放在我们的评测内容上。老实说,我们对玩游戏的建议设置与我们的评测设置非常相似,除

    2022年5月21日
    51
  • 想自学Java的速来!孙鑫视频教程百度云盘

    想自学Java的速来!孙鑫视频教程百度云盘MySQL数据库最佳学习线路脑图:一、对MySQL的认识认识Mysql数据库下载安装MySQL软件在Linux系统环境下安装MySQLMySOL体系结构与存储引擎MySQL体系结构QueryCache详解存储引擎InnoDB体系结构InnoDB的三大特性.数据库文件参数文件参数类型错误日志文件(errorlog)二进制日志文件(binarylog)慢查询日志(slowlog)全量日志(generallog)审计日志(auditlog)

    2022年5月16日
    38
  • java策略模式例子_java 登录场景 策略模式

    java策略模式例子_java 登录场景 策略模式Java的策略模式中体现了两个非常基本面向对象的原则-封装变化的概念-编程中使用接口,而不是对接口的实现策略模式的定义定义一组算法,将每个算法都封装起来,并且使它们之间可以互换。策略模式使这些算法在客户端调用它们的时候能够互不影响的变化策略模式的组成抽象策略角色–策略类,通常由一个接口或者抽象类实现具体策略角色–包装了相

    2025年9月7日
    11
  • Windows套接字CAsyncSocket类的使用「建议收藏」

    Windows套接字CAsyncSocket类的使用「建议收藏」TCP编程的服务器端一般步骤是:1、创建一个CAsyncSocket或CSocket类或其子类对象A,用于监听客户端连接,然后通过Create函数设定端口号和协议类型为SOCK_STREAM。2、开启监听,用函数Listen();3、接收客户端上来的连接,用函数Accept(&B);并且把连接连接的对象存到B中,以便通信使用。4、当客户端发送来消息时,会触发OnReceive函数…

    2022年8月18日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号