元组类型内容python

Vik*_*ren 1 python types tuples instanceof

我可以以某种方式看到元组内容的类型和大小吗?我希望输出类似于:

(str, list, int) 或者任何可能的东西(只是打印它们很难,因为有嵌套列表)

x = someSecretTupleFromSomewhereElse
print type(x)

<(type 'tuple')>
Run Code Online (Sandbox Code Playgroud)

jam*_*lak 9

>>> data = ('ab', [1, 2, 3], 101)
>>> map(type, data)
[<type 'str'>, <type 'list'>, <type 'int'>]
Run Code Online (Sandbox Code Playgroud)

要显示您可以执行此操作的长度,对于不是我将要显示的序列的项目None.

>>> import collections
>>> [(type(el), len(el) if isinstance(el, collections.Sequence) else None)
     for el in data]
[(<type 'str'>, 2), (<type 'list'>, 3), (<type 'int'>, None)]
Run Code Online (Sandbox Code Playgroud)

  • 不知道你可以映射元组,很酷:D (2认同)