【发布时间】:2017-07-05 16:11:20
【问题描述】:
我有这个输出
(10.76, 27.73)
我想将它作为一个值分配给一个键,这是一个代码
dictionary = {'whatever':'whatever, 'KEY':'I need this output(tuple) here'}
另外,小细节,我宁愿保留逗号,但如果不可能,我会自己添加它,没关系。
谢谢
【问题讨论】:
标签: python dictionary key tuples
我有这个输出
(10.76, 27.73)
我想将它作为一个值分配给一个键,这是一个代码
dictionary = {'whatever':'whatever, 'KEY':'I need this output(tuple) here'}
另外,小细节,我宁愿保留逗号,但如果不可能,我会自己添加它,没关系。
谢谢
【问题讨论】:
标签: python dictionary key tuples
mytuple = (10.76, 27.73)
dictionary = {'whatever':'whatever', 'KEY':mytuple}
{'KEY': (10.76, 27.73), 'whatever': 'whatever'}
或
mytuple = (10.76, 27.73)
dictionary = {'whatever':'whatever', 'KEY':str(mytuple)}
{'KEY': '(10.76, 27.73)', 'whatever': 'whatever'}
【讨论】:
只需分配它。
>>> t = (10.76, 27.73)
>>> d = {}
>>> d['key'] = t
>>> d
{'key': (10.76, 27.73)}
【讨论】: