0

ファイル名のリストが与えられた場合、拡張子 hpp を持つすべてのファイルの名前を拡張子 h に変更します。これを行うには、新しいファイル名で構成される newfilenames という名前の新しいリストを生成します。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.
new_filename=[]
new_list=[]
final_file=[]
for element in filenames:
    if element.endswith("p"):
        new_filename.append(element)
for element1 in new_filename:
    new_list.append(element1.split("pp")[0])
for element3 in filenames:
    if not element3.endswith("p"):
        final_file.append(element3)
final_file.extend(new_list)
print(final_file)
# Should be ["program.c", "stdio.h", "sample.h", "a.out", "math.h", "hpp.out"]

index[1] で new_list を final_file に拡張する方法はありますか?

もっと簡単な解決策はありますか?

4

1 に答える 1