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


相关推荐

  • xiao zhang   jia you[通俗易懂]

    xiao zhang   jia you[通俗易懂]加油! 转载于:https://blog.51cto.com/755633522/542479

    2022年10月6日
    0
  • 每天一道算法_8_DNA Sorting

    DescriptionOne measure of “unsortedness” in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence “DAABEC”, this mea

    2022年3月10日
    38
  • 打印菱形Java_for循环打印菱形

    打印菱形Java_for循环打印菱形Java打印菱形Java打印菱形先看效果:题目分析代码Java打印菱形先看效果:题目分析1、星号前面的空白要用空格代替。2、把图形分为上下两部分,分别找出行数与“空格”和“*”的关系上半部分(正三角):空格个数与行数的关系橘色框:代表行号。红色框:正三角形成的最大行号(maxNum)。计算公式:(总行数+1)/2&nbsp;&nbsp;&nbsp;&nbsp;&nb…

    2022年9月29日
    0
  • 使用Apache服务部署静态网站

    1.网站服务程序windows系统中默认Web服务程序是IIS(InternetInformationServices),这是一款图形化的网站管理工具,IIS程序不光能提供Web网站服务,还能

    2021年12月28日
    40
  • ODrive 通讯协议「建议收藏」

    ODrive通讯协议与ODrive进行通讯需要对通讯端点进行一系列操作。理论上,端点上的数据可以是以任何方式序列化的任何类型的数据。数据包采用默认的序列化方式,对于您自定义的数据包,您必须自己去进行反序列化。未来我们可能会提供序列化功能。可以通过从端点0读取JSON来枚举可用的端点,从理论上讲,每个接口都可以不同(实际上并没有这么做)。每个端点都可以被用来发送和接收字节数据,有效字节数据的含义在…

    2022年4月17日
    41
  • 万能头文件可能产生的副作用_头文件使用std

    万能头文件可能产生的副作用_头文件使用std可能有些大型比赛会禁止使用这个头文件,我个人建议,大家尽量还是熟悉原来的文件比较好哈,要是比赛时实在忘了可以应急使用最近在打一些比赛,翻阅别人的代码时总是会发现一个陌生而奇怪的头文件#include<bits/stdc++.h>奇怪之处就在于基本上所有的代码只要用了这个头文件就不再写其他头文件了。百度过后仿佛打开了新世界的大门,头文件居然还可以这样用!!!#include&lt…

    2022年8月18日
    5

发表回复

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

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