在 Python 中获取元组元素数据类型
如果需要获取元组元素的数据类型,可以使用“映射”方法和“类型”方法。
映射函数将给定的函数/操作应用于可迭代对象(例如,列表、元组)中的每个项目。它返回一个列表作为结果。
“类型”方法返回传递给它的元素类型的类型。
列表可用于存储异构值(即,任何数据类型的数据,例如整数、浮点数、字符串等)。
以下是对它的演示 -
示例
my_tuple = ('Hi', 23, ['there', 'Will'])
print("The tuple is : ")
print(my_tuple)
my_result = list(map(type, my_tuple))
print("The data types of tuple in order are : ")
print(my_result)输出
The tuple is :
('Hi', 23, ['there', 'Will'])
The data types of tuple in order are :
[<class 'str'>, <class 'int'>, <class 'list'>]说明
- 定义包含列表的元组,并将其显示在控制台上。
- 使用映射方法将“类型”方法应用于列表元组的所有元素。
- 将其转换为列表。
- 将其分配给一个值。
- 它显示在控制台上。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP