问题描述
filenames = ["program.c","stdio.hpp","sample.hpp","a.out","math.hpp","hpp.out"]
new_filename=[]
new_list=[]
final_file=[]
for element in filenames:
if element.endswith("p"):
new_filename.append(element)
提取所有以.hpp结尾的文件名 对于new_filename中的element1: new_list.append(element1.split(“ pp”)[0]) 将new_filename中的元素的.hpp转换为.h 对于文件名中的element3: 如果不是,那么element3.endswith(“ p”): final_file.append(element3) 提取非.hpp文件名 final_file.extend(new_list) 打印(最终文件) #应该为[“ program.c”,“ stdio.h”,“ sample.h”,“ a.out”,“ math.h”,“ hpp.out”] #输入中的输出正在获取['program.c','a.out','hpp.out','stdio.h','sample.h','math.h']
# is there any way to extend the new_list to final_file in the index 1 ?
解决方法
关于“将新列表扩展到最终文件”;您可以像这样在某个插入点将列表插入另一个列表:
l1 = ['a','b','c']
l2 = ['x','y','z']
l1[1:1] = l2 # ['a','x','z','c']