生成内置类型值的字典理解
Dictionary Comprehension to generate values of built-in type
我想做一个字典理解来获得一个键列表,其中内置类型 str
作为值。
headers = ['Header1', 'Header2', 'Header3']
print dict([(x,str) for x in headers])
输出:
{'Header2': <type 'str'>, 'Header3': <type 'str'>, 'Header1': <type 'str'>}
期望的输出:
{'Header2': str, 'Header3': str, 'Header1': str}
你有一本内置str
的字典。
<type 'str'>
是由于 print
调用在打印时使用从调用对象的 __str__
获得的值。 str
的值是 <type 'str'>
。
如果您保存字典,访问其中一个成员并使用它,您会看到它 是 str
class:
>>> d = dict([(x,str) for x in headers])
>>> d['Header1'](123)
'123'
我想做一个字典理解来获得一个键列表,其中内置类型 str
作为值。
headers = ['Header1', 'Header2', 'Header3']
print dict([(x,str) for x in headers])
输出:
{'Header2': <type 'str'>, 'Header3': <type 'str'>, 'Header1': <type 'str'>}
期望的输出:
{'Header2': str, 'Header3': str, 'Header1': str}
你有一本内置str
的字典。
<type 'str'>
是由于 print
调用在打印时使用从调用对象的 __str__
获得的值。 str
的值是 <type 'str'>
。
如果您保存字典,访问其中一个成员并使用它,您会看到它 是 str
class:
>>> d = dict([(x,str) for x in headers])
>>> d['Header1'](123)
'123'