【问题标题】:why my double quoted list elements changed to single quote after modification?为什么我的双引号列表元素在修改后变为单引号?
【发布时间】:2020-06-24 18:47:15
【问题描述】:
filenames = ["program.c", "stdio.hpp", "sample.hpp", "a.out", "math.hpp", "hpp.out"]
# Generate newfilenames as a list containing the new filenames
# using as many lines of code as your chosen method requires.
newfilenames = [i.replace("hpp","h") for i in filenames]

print(newfilenames) 

我的输出是['program.c', 'stdio.h', 'sample.h', 'a.out', 'math.h', 'h.out']

我的要求是["program.c", "stdio.h", "sample.h", "a.out", "math.h", "hpp.out"]

这意味着在上面的单引号中我需要用双引号替换它。

【问题讨论】:

  • 在语法上,单引号和双引号没有区别,所以两个输出在功能上是一样的。
  • 他们没有改变;您正在查看列表元素的表示,因为str(newfilesnames) 使用repr(而不是str)来获取列表中每个元素的字符串表示。
  • 您所需的输出看起来很像 JSON,这意味着您应该改用 print(json.dumps(newfilenmes))

标签: python list methods


【解决方案1】:

Python 不区分单引号和双引号。

这里有更多信息-> https://www.geeksforgeeks.org/single-and-double-quotes-python/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 2010-09-20
    • 1970-01-01
    • 2013-05-26
    • 2015-12-18
    • 1970-01-01
    • 2019-05-17
    相关资源
    最近更新 更多