我有以下代码,它运行得很好.然而,由于我对Python非常陌生,仍有改进的空间.我希望你能向我建议处理这一特殊情况的最佳方法,同时,我可以从中吸取教训.
# Find all methods belonging to this header
custom_methods_of_the_current_header = [method for method in fmt_data if
method["type"] == constants.CUSTOM_METHOD and method[
"parent_id"] == header_id]
custom_methods_of_the_current_header_ordered = []
for i in range(1, len(ordered_list) - 1):
for j in range(len(custom_methods_of_the_current_header)):
if custom_methods_of_the_current_header[j]["name"] == ordered_list[i].gg.kind:
custom_methods_of_the_current_header_ordered.insert(i - 1,
custom_methods_of_the_current_header[j])
else:
continue
The code there are 3 lists.
custom_methods_of_the_current_header list is getting every matches item and adds inside. ex ["header01", "header03", "header02"]
ordered_list是 list 上有订购的项目EX.["mainHeader","Header03","Header01","Header02","Footer"],因为主页眉和页脚并不重要,所以开始的范围是1到3
custom_methods_of_the_current_header_ordered列表我用来添加最终的有序列表,方法是将Custom_Methods_of_the_Current_Header与Order_List进行比较,因为Order_List中的Header03应该是第一个,然后在当前Header列表中找到Header03并将其添加到第一个索引[0,Header03]中
代码本身工作并完成了我所需要的,然而,我想知道我是否可以优化它,使它看起来更干净,并减少循环和复杂性.提前谢谢你