【问题标题】:Transform python dict to string将python dict转换为字符串
【发布时间】:2020-09-01 06:47:26
【问题描述】:

给定 Python 字典

x = {
  'x': '1',
  'y': '2',
  'z': '3',
}

将其转换为字符串的代码是什么

x=1 y=2 z=3 

【问题讨论】:

  • ' '.join(f'{k}={v}' for k, v in x.items())
  • ' '.join('{0}={1}'.format(k, v) for k, v in your_dict.items())

标签: python string dictionary


【解决方案1】:
s = ' '.join(f'{key}={value}' for (key, value) in x.items())

应该做的伎俩。

但要小心包含空格的键或值! :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-25
    • 1970-01-01
    • 2011-05-30
    • 2018-07-27
    • 1970-01-01
    • 2019-03-27
    相关资源
    最近更新 更多