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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • UML图画法_画用例图的步骤

    UML图画法_画用例图的步骤一.用例图的作用用例图主要用来描述“用户、需求、系统功能单元”之间的关系。它展示了一个外部用户能够观察到的系统功能模型图。【用途】:帮助开发团队以一种可视化的方式理解系统的功能需求。二.用例图包含的元素 1.参与者(Actor)  表示与您的应用程序或系统进行交互的用户、组织或外部系统。用一个小…

    2022年9月7日
    0
  • ubuntu域名服务器配置_linux虚拟主机配置

    ubuntu域名服务器配置_linux虚拟主机配置像我一样刚开始接触Ubuntu系统朋友不知道怎么配置虚拟主机的可以看一下,这里完整记录了我配置虚拟主机的全部过程

    2022年9月2日
    2
  • android 图片识别文字,安卓手机如何识别图片中的文字?一个方法轻松解决难题…

    android 图片识别文字,安卓手机如何识别图片中的文字?一个方法轻松解决难题…现在使用安卓手机的人并不少,有时在工作生活中,需要利用安卓手机将图片中的文字识别提取出来,这个时候你会吗?相信很多人的答案是否定的,那么安卓手机如何识别图片中的文字呢?下面我们就一起来看看吧。想要利用安卓手机将图片中的文字识别提取出来,你只需要这样做就行:很简单,只要在安卓手机上下载安装一个专门的图片文字识别APP即可。那这个图片文字识别APP是什么呢?现在图片文字识别APP是很多,小编比较常用的…

    2022年4月28日
    73
  • python类型转换astype时间_python dataframe astype 字段类型转换方法

    python类型转换astype时间_python dataframe astype 字段类型转换方法使用astype实现dataframe字段类型转换#-*-coding:UTF-8-*-importpandasaspddf=pd.DataFrame([{‘col1′:’a’,’col2′:’1′},{‘col1′:’b’,’col2′:’2′}])printdf.dtypesdf[‘col2’]=df[‘col2’].astype(‘int’)print’–…

    2022年6月5日
    43
  • ajax parsererror报错,jQuery为ajax请求返回“ parsererror”[通俗易懂]

    ajax parsererror报错,jQuery为ajax请求返回“ parsererror”[通俗易懂]我一直在从jquery收到针对Ajax请求的“parsererror”,我尝试将POST更改为GET,以几种不同的方式(创建类等)返回数据,但我似乎无法弄清楚问题出在哪里。我的项目在MVC3中,我使用的是jQuery1.5,我有一个Dropdown,并且在onchange事件上,我触发了一个调用,以根据所选内容获取一些数据。下拉列表:(这会从Viewbag的列表中加载“Views”,并触发事…

    2022年6月29日
    20
  • 微信自动发送消息

    微信自动发送消息前提:今天加入微信辅助大军,奈何要一直去群里发广告,又懒又烦!!!于是乎,想到能不能自动去打广告~可以的~哈哈方案:最近在看api文档,就最先想到能不能java模拟发送信息,但是又没头绪(放弃)然后百度了相关信息,得出①脚本精灵录制(直接趴,这不是自己编程该有的风格)②微信网页版(js脚本)大致思路:1、遍历好友列表(避免发错

    2022年6月4日
    44

发表回复

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

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