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 13:07:35 浏览: 74
可以将重复的代码段封装成一个函数,传入不同的参数来实现不同的功能,避免重复代码。例如:
```python
def count_conditions(teacher_arr, condition_func, time_func, count):
count = count_conditions(teacher_arr, condition_func, lambda row: row[8] == itme)
if count != 0:
sh_4149.cell(index1 6, index 4).value = count
# 使用方法
count_conditions(teacher_arr, lambda row: row[9] == '学前' or row[9] == '小学', lambda row: row[2] == '女', '女')
count_conditions(teacher_arr, lambda row: row[11] != '汉族', lambda row: row[12] == '是', '少数民族')
```
相关问题
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代码优化
:
首先,可以将每一个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
if itme1 == '小学': count = count_conditions( change_student_arr, lambda row: row[6] == itme1, lambda row: row[4] == itme) if count != 0: sh_3040.cell(index1+7, index+6).value = count if itme1 == '女': count = count_conditions( change_student_arr, lambda row: row[2] == itme1, lambda row: row[4] == itme) if count != 0: sh_3040.cell(index1+7, index+6).value = count if itme1 == '少数民族': count = count_conditions( change_student_arr, lambda row: row[3] == itme1, lambda row: row[4] == itme) if count != 0: sh_3040.cell(index1+7, index+6).value = count代码优化
ChitGPT:您好!关于您的代码,我可以提供一些改进意见。首先,可以将多个if语句合并成一个,以减少冗余代码。具体来说,请尝试以下改进:
count = 0
if itme1 == '小学':
count = count_conditions(change_student_arr, lambda row: row[6] == itme1, lambda row: row[4] == itme)
elif itme1 == '女':
count = count_conditions(change_student_arr, lambda row: row[2] == itme1, lambda row: row[4] == itme)
elif itme1 == '少数民族':
count = count_conditions(change_student_arr, lambda row: row[3] == itme1, lambda row: row[4] == itme)
if count != 0:
sh_3040.cell(index1 7, index 6).value = count
这样可以让代码更加简洁清晰,同时还能提高代码的可读性和可维护性。如果您有任何问题或者需要我帮忙进行其他的改进,欢迎随时告诉我。
阅读全文