【问题标题】:What placeholder is used for list in python?python中的列表使用什么占位符?
【发布时间】:2019-11-15 09:45:38
【问题描述】:

Python 中的列表是否有格式占位符,例如,“%s”是字符串的格式占位符,“%d”是整数的占位符?

我有一个清单:

result_list = [{"a": "EC2", "b": 300}, {"a": "VPC", "b": 102}, {"a": "S3", "b": 140}]

并且想把列表的内容放到一个字符串中。如果results_list 本身就是我可以使用的字符串:

my_str = "input_list = %s"
print(my_str % (result_list))

但鉴于result_list 是一个列表,我该怎么做呢?

【问题讨论】:

  • 你期望得到什么输出?
  • 我已经编辑了这个问题。现在会更清楚了
  • 如果有的话,现在不太清楚
  • %s 做了什么你意想不到的事?
  • 您期望/想要达到什么结果?

标签: python python-3.x list


【解决方案1】:

您可以使用字符串格式的方法,例如:

result_list = [{"a": "EC2", "b": 300}, {"a": "VPC", "b": 102}, {"a": "S3", "b": 140}]

my_str = "input_list: {}"
print(my_str.format(result_list))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 2019-06-04
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    相关资源
    最近更新 更多