[英]how to modify element of list in python
Given a list of filenames, we want to rename all the files with extension.hpp to the extension h.给定文件名列表,我们希望将所有扩展名为.hpp 的文件重命名为扩展名 h。 To do this, we would like to generate a new list called newfilenames, consisting of the new filenames.为此,我们想生成一个名为 newfilenames 的新列表,其中包含新文件名。 Fill in the blanks in the code using any of the methods you've learned thus far, like a for loop or a list comprehension.使用您迄今为止学到的任何方法来填写代码中的空白,例如 for 循环或列表推导式。
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.
print(newfilenames)
# Should be ["program.c", "stdio.h", "sample.h", "a.out", "math.h", "hpp.out"]
newfilenames = [e.replace('.hpp','.h') for e in filenames]
filenames = ["program.c", "stdio.hpp", "sample.hpp", "a.out", "math.hpp", "hpp.out"]
newfilenames = []
for names in filenames:
if names.endswith('.hpp'):
newfilenames.append(names[:-2])
continue
newfilenames.append(names)
print(newfilenames)
filenames = ["program.c", "stdio.hpp", "sample.hpp", "a.out", "math.hpp", "hpp.out"]
newfilenames=[]
for x in filenames:
if x.endswith(".hpp"):
newfilenames.append(x.replace(".hpp",".h"))
else:
newfilenames.append(x)
print(newfilenames)
filenames = ["program.c", "stdio.hpp", "sample.hpp", "a.out", "math.hpp", "hpp.out"]
newfilenames=[]
for filename in filenames:
if '.hpp' in filename:
index=filename.index(".")
newfile=filename[:index]+".h"
newfilenames.append(newfile)
else:
newfilenames.append(filename)
print(filenames)
filenames = ["program.c", "stdio.hpp", "sample.hpp", "a.out", "math.hpp", "hpp.out"]
newfilenames=[]
for filename in filenames:
if filename.endswith(".hpp"):
filename = filename.replace(".hpp", ".h")
newfilenames.append(filename)
else:
newfilenames.append(filename)
print(newfilenames)
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 = [word.replace("hpp","h") if word.endswith("hpp") else word for word in filenames ] print(newfilenames) # Should be ["program.c", "stdio.h", "sample.h", "a.out", "math.h", "hpp.out"]
newfilenames = [ f.rstrip("pp") if f.endswith(".hpp") else f for f in filenames ]
Instead of str.replace()
, I prefer using str.rstrip()
and str.endswith()
to take care of potential edge cases.而不是 str.replace( str.replace()
,我更喜欢使用str.rstrip()
和str.endswith()
来处理潜在的边缘情况。
newfilenames = []
for names in filenames:
if names.endswith('.hpp'):
newfilenames.append(names[:-2])
continue
newfilenames.append(names)
def pig_latin(text):
v = text.split()
t = []
for x in v:
i = str(x[1:]) + str(x[0][0]) + 'ay'
t.append(i)
return t
print(pig_latin("hello how are you")) # Should be "ellohay owhay reaay ouyay"
print(pig_latin("programming in python is fun")) # Should be "rogrammingpay niay ythonpay siay unfay"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.