filenames = ["program.c", "stdio.hpp", "sample.hpp", "a.out", "math.hpp", "hpp.out"]我想用 .h 替換 .hpp。y=[]for x in filenames: if '.hpp' in x: x=x.replace('hpp','h') y.append(x) else: y.append(x)print(y)有沒有更簡單的方法來做到這一點?
列表中項目的字符串替換
慕田峪7331174
2023-12-05 14:58:15