タプルの内容のタイプとサイズをどうにかして確認できますか?次のような出力が欲しいのですが。
(str, list, int)
または可能なことは何でも(ネストされたリストがあるので、それらを印刷するだけでは難しいです)
x = someSecretTupleFromSomewhereElse
print type(x)
<(type 'tuple')>
タプルの内容のタイプとサイズをどうにかして確認できますか?次のような出力が欲しいのですが。
(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)]