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)
上一篇 2022年6月7日 上午11:00
下一篇 2022年6月7日 上午11:00


相关推荐

  • 解决pycharm中使用pip安装numpy失败的问题「建议收藏」

    解决pycharm中使用pip安装numpy失败的问题「建议收藏」今天使用pycharm编译python程序时,由于要调用numpy包,但又未曾安装numpy,于是就根据pycharm的提示进行安装,最后竟然提示出错!!!如下图:这不是要让我回归命令行的生活吗?!解决方案如下:1、下载numpy-1.19.5-cp39-cp39-win_amd64.whl,网址是https://pypi.org/project/numpy/#files2、将下载好的numpy文件放在python安装路径下的/scripts中3、在命令行状态下切换到script

    2022年8月29日
    9
  • 2025最全Cherry Studio使用GPT-4o-transcribe指南:8种应用场景提升效率【实战教程】

    2025最全Cherry Studio使用GPT-4o-transcribe指南:8种应用场景提升效率【实战教程】

    2026年3月15日
    4
  • arduino中Keypad 库函数介绍

    arduino中Keypad 库函数介绍原文:https://playground.arduino.cc/Code/Keypad/Creation构造函数:Keypad(makeKeymap(userKeymap),row[],col[],rows,cols)constbyterows=4;//fourrowsconstbytecols=3;//threecolumnscharkeys[rows][cols]={{‘1′,’2′,’3’},{‘4′,’5′,’6’},{‘

    2022年6月7日
    38
  • 跨平台长连接组件设计及可插拔改造

    跨平台长连接组件设计及可插拔改造背景我们在提出开发跨平台组件之前,iOS和Android客户端分别使用一套长连接组件,需要双倍的人力开发和维护;在产品需求调整上,为了在实现细节上保持一致性也具有…

    2022年5月29日
    47
  • 进程之间的通信方式「建议收藏」

    进程之间的通信方式「建议收藏」进程间通信方式一般有以下几种:1、管道,匿名管道,命名管道2、信号3、信号量4、消息队列5、共享内存6、socket管道管道数据只能单向流动,所以如果要实现双向通信,就要创建2个管道管道分为匿名管道和命名管道匿名管道只能在父子进程关系之间使用命名管道,可以在不关联的两个进程之间使用,因为它创建了一个类型为管道的设备文件,使用这个设备文件就可以通信。管道只能承载无格式的字节流信号信号是进程之间唯一的异步通信机制,信号的主要来源主要有硬件来源(入键盘操作ctrl+C)

    2022年10月11日
    9
  • iOS关于地图定位基础(二)[通俗易懂]

    iOS关于地图定位基础(二)[通俗易懂]在前一篇文章 iOS关于地图定位基础(一) 中我们主要总结了 iOS里面利用原生 CoreLocation 框架实现基本定位功能和一些注意点,侧重点主要是iOS8+之后的定位授权与授权状态的使用。接下来本篇文章主要是讲解如何利用 CoreLocation 框架实现地理定位的具体实现。(PS:下文涉及我自定义的指南针Demo请去我的GitHub仓库查看源码https://github.com/IML

    2022年7月26日
    7

发表回复

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

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