【发布时间】: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))。