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 12:07:21 浏览: 91
:可以考虑将重复使用的代码封装成一个函数,以减少代码重复和提高代码可读性。例如:
def count_by_criteria(teacher_arr, criteria_func, filter_func):
count = count_conditions(teacher_arr, criteria_func, filter_func)
if count != 0:
sh_4149.cell(index1, index).value = count
然后使用这个函数来代替原本重复的代码,例如:
if itme1 == '总计':
count_by_criteria(teacher_arr, lambda row: row[9] == '学前' or row[9] == '小学', lambda row: row[8] == itme)
if itme1 == '女':
count_by_criteria(teacher_arr, lambda row: row[2] == '女', lambda row: row[8] == itme)
if itme1 == '少数民族':
count_by_criteria(teacher_arr, lambda row: row[11] != '汉族', lambda row: row[8] == itme)
if itme1 == '在编人员':
count_by_criteria(teacher_arr, lambda row: row[12] == '是', lambda row: row[8] == itme)
这样代码会更易读和易维护。
阅读全文