【问题标题】:Convert a arbitrarily nested list of literals to string representation [duplicate]将任意嵌套的文字列表转换为字符串表示形式[重复]
【发布时间】:2016-07-26 02:46:14
【问题描述】:

如何将任意嵌套的文字(字符串/数字)列表转换为其字符串表示形式? 例如,

nested_list = [[1,2,4],[1, [1, 2], [1]],2]
nested_list_string = "[[1,2,4],[1, [1, 2], [1]],2]"

此外,它可以是多行:

[
    1, 2,
    [2,4]
]

可以产生等效单行列表的字符串表示,即"[1,2, [2,4]]"或任何其他等效形式。

【问题讨论】:

    标签: python string serialization


    【解决方案1】:

    使用str 执行此操作。

    str(nested_list)
    

    【讨论】:

      猜你喜欢
      • 2016-02-08
      • 1970-01-01
      • 2021-11-25
      • 2017-11-24
      • 2021-12-01
      • 1970-01-01
      • 2019-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多