我能否以某种方式查看元组内容的类型和大小?我想要这样的输出:
(str, list, int) 或任何可能的方法(由于存在嵌套列表,仅打印它们很困难)
x = someSecretTupleFromSomewhereElse
print type(x)
<(type 'tuple')>
请您参考如下方法:
>>> data = ('ab', [1, 2, 3], 101)
>>> map(type, data)
[<type 'str'>, <type 'list'>, <type 'int'>]
要同时显示长度,您可以执行此操作,对于不是序列的项目,我将只显示 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)]
