【问题标题】:How can I assign an output(tuple) into a key value? Python如何将输出(元组)分配给键值? Python
【发布时间】:2017-07-05 16:11:20
【问题描述】:

我有这个输出

(10.76, 27.73)

我想将它作为一个值分配给一个键,这是一个代码

dictionary = {'whatever':'whatever, 'KEY':'I need this output(tuple) here'} 

另外,小细节,我宁愿保留逗号,但如果不可能,我会自己添加它,没关系。

谢谢

【问题讨论】:

    标签: python dictionary key tuples


    【解决方案1】:

    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'}

    【讨论】:

      【解决方案2】:

      只需分配它。

      >>> t = (10.76, 27.73)
      >>> d = {}
      >>> d['key'] = t
      >>> d
      {'key': (10.76, 27.73)}
      

      【讨论】:

        猜你喜欢
        • 2019-11-11
        • 2017-06-29
        • 2021-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多