Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在集合中查找对象的类型?_Python - Fatal编程技术网

Python 如何在集合中查找对象的类型?

Python 如何在集合中查找对象的类型?,python,Python,如何使用单行代码查找集合中的对象类型 setup = {(1,2),(3,4),(5,6)} >>> type(setup.____) <class 'int'> setup={(1,2)、(3,4)、(5,6)} >>>类型(设置) 也许吧?真的不清楚你的问题是什么。。。仅仅因为集合中的一个元素是元组并不意味着集合中的所有元素都是元组。如果要获取元组中每个元素的类型,可以使用列表理解,如: [type(x) for elem in setup for x

如何使用单行代码查找集合中的对象类型

setup = {(1,2),(3,4),(5,6)}

>>> type(setup.____)

<class 'int'>
setup={(1,2)、(3,4)、(5,6)}
>>>类型(设置)

也许吧?真的不清楚你的问题是什么。。。仅仅因为集合中的一个元素是元组并不意味着集合中的所有元素都是元组。

如果要获取元组中每个元素的类型,可以使用列表理解,如:

[type(x) for elem in setup for x in elem]
输出:

[<type 'int'>, <type 'int'>, <type 'int'>, <type 'int'>, <type 'int'>, <type 'int'>]
要获得以下输出:

[[<type 'int'>, <type 'int'>], [<type 'int'>, <type 'int'>], [<type 'int'>, <type 'int'>]]


哪个对象的类型?
集中所有对象的类型都是
tuple
,而不是
int
。如果有不同的类型呢?为什么它必须在一行上?
[type(s[0])对于设置中的s][0]
如果您不介意浪费一点内存的话。@Shiva使用
next
type(next(s[0])对于设置中的s))
来节省一些内存和时间。
只是一个猜测,但您是否正在尝试查看您的所有数据是否都是同质的?这似乎有点像XY问题。集合可以容纳许多对象,它们都可以是不同的类型,所以就目前而言,你的问题没有什么意义。求你了,做吧,这样人们就不用猜你的意思了。
[[type(x) for x in elem] for elem in setup]
[[<type 'int'>, <type 'int'>], [<type 'int'>, <type 'int'>], [<type 'int'>, <type 'int'>]]