Python 中的 type(), dtype(), astype()的区别
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() |
1 | import numpy as np |
下面的示例定义一个结构化数据类型 student,包含字符串字段 name,整数字段 age,及浮点字段 marks,并将这个 dtype 应用到 ndarray 对象。
1 | import numpy as np |
输出结果为:
1 | [('name', 'S20'), ('age', 'i1'), ('marks', 'f4')] |
1 | import numpy as np |
输出结果为:
1 | [('abc', 21, 50.0), ('xyz', 18, 75.0)] |
参考链接:
https://blog.csdn.net/Strive_For_Future/article/details/92811293
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 饮茶!
评论