if index==1: count = count_conditions( teacher_arr, lambda row: row[3] <24, lambda row: row[11] == itme1, lambda row: row[8] == '专任教师') pass if index==2: count = count_conditions( teacher_arr, lambda row: row[3] >24 and lambda row: row[3] <30, lambda row: row[11] == itme1, lambda row: row[8] == '专任教师') pass if index==3: count = count_conditions( teacher_arr, lambda row: row[3] >29 and lambda row: row[3] <35, lambda row: row[11] == itme1, lambda row: row[8] == '专任教师') pass if index==4: count = count_conditions( teacher_arr, lambda row: row[3] >34 and lambda row: row[3] <40, lambda row: row[11] == itme1, lambda row: row[8] == '专任教师') pass if index==5: count = count_conditions( teacher_arr, lambda row: row[3] >39 and lambda row: row[3] <45, lambda row: row[11] == itme1, lambda row: row[8] == '专任教师') pass if index==6: count = count_conditions( teacher_arr, lambda row: row[3] >44 and lambda row: row[3] <50, lambda row: row[11] == itme1, lambda row: row[8] == '专任教师') pass if index==7: count = count_conditions( teacher_arr, lambda row: row[3] >49 and lambda row: row[3] <55, lambda row: row[11] == itme1, lambda row: row[8] == '专任教师') pass if index==8: count = count_conditions( teacher_arr, lambda row: row[3] >54 and lambda row: row[3] <60, lambda row: row[11] == itme1, lambda row: row[8] == '专任教师') pass if index==8: count = count_conditions( teacher_arr, lambda row: row[3] >54 and lambda row: row[3] <60, lambda row: row[11] == itme1, lambda row: row[8] == '专任教师') pass if index==9: count = count_conditions( teacher_arr, lambda row: row[3] >=60, lambda row: row[11] == itme1, lambda row: row[8] == '专任教师') pass 优化代码
时间: 2023-06-09 16:07:32 浏览: 82
mysql_fetch_row,mysql_fetch_array,mysql_fetch_assoc的区别
您的代码可以通过使用循环和列表,来避免重复的代码。以下是一种重构方案:
```
age_ranges = [(0, 24), (24, 30), (30, 35), (35, 40), (40, 45), (45, 50), (50, 55), (55, 60), (60, 100)]
counts = []
for i, age_range in enumerate(age_ranges):
if i == index - 1:
count = count_conditions(teacher_arr,
lambda row: age_range[0] < row[3] < age_range[1],
lambda row: row[11] == item1,
lambda row: row[8] == '专任教师')
counts.append(count)
else:
counts.append(0)
```
这样,您就可以通过循环和列表,避免重复的代码,并且可以更加清晰和易读。
阅读全文