for index1, itme1 in enumerate(list_col): for index, itme in enumerate(list_row): if itme1 == '总计': count = count_conditions( teacher_arr, lambda row: row[9] == '学前' or row[9] == '小学', lambda row: row[8] == itme) if count != 0: sh_4149.cell(index1+6, index+4).value = count if itme1 == '女': count = count_conditions( teacher_arr, lambda row: row[2] == '女', lambda row: row[8] == itme) if count != 0: sh_4149.cell(index1+6, index+4).value = count if itme1 == '少数民族': count = count_conditions( teacher_arr, lambda row: row[11] != '汉族', lambda row: row[8] == itme) if count != 0: sh_4149.cell(index1+6, index+4).value = count if itme1 == '在编人员': count = count_conditions( teacher_arr, lambda row: row[12] == '是', lambda row: row[8] == itme) if count != 0: sh_4149.cell(index1+6, index+4).value = count代码优化
时间: 2023-06-09 11:07:33 浏览: 121
charts:由Itme发布的Helm图表(主要与Solid相关)
:
首先,可以将每一个if语句的判断条件都提前判断一遍,如果不满足就直接跳过,避免重复计算和赋值。
其次,可以将count_conditions函数的每个参数使用lambda函数封装成一个函数参数,以简化代码。
最后,可以将重复的赋值操作提取出来,避免重复代码。
优化后的代码如下:
count_conditions_filter1 = lambda row: row[9] == '学前' or row[9] == '小学'
count_conditions_filter2 = lambda row: row[2] == '女'
count_conditions_filter3 = lambda row: row[11] != '汉族'
count_conditions_filter4 = lambda row: row[12] == '是'
for index1, item1 in enumerate(list_col):
count = 0
if item1 == '总计':
count = count_conditions(teacher_arr, count_conditions_filter1, lambda row: row[8] == item)
elif item1 == '女':
count = count_conditions(teacher_arr, count_conditions_filter2, lambda row: row[8] == item)
elif item1 == '少数民族':
count = count_conditions(teacher_arr, count_conditions_filter3, lambda row: row[8] == item)
elif item1 == '在编人员':
count = count_conditions(teacher_arr, count_conditions_filter4, lambda row: row[8] == item)
if count != 0:
sh_4149.cell(index1 6, index 4).value = count
阅读全文