django代码优化全局变量定义
需要根据不同年级的学生肺活量进行分数获取,在根据分数*权重得到最终分数。不同年级权重不同
旧代码定义
#### 肺活量,权重0.15 calculate_lung_100 = 100 * 0.15 calculate_lung_95 = 95 * 0.15 calculate_lung_90 = 90 * 0.15 calculate_lung_85 = 85 * 0.15 calculate_lung_80 = 80 * 0.15 calculate_lung_78 = 78 * 0.15 calculate_lung_76 = 76 * 0.15 calculate_lung_74 = 74 * 0.15 calculate_lung_72 = 72 * 0.15 calculate_lung_70 = 70 * 0.15 calculate_lung_68 = 68 * 0.15 calculate_lung_66 = 66 * 0.15 calculate_lung_64 = 64 * 0.15 calculate_lung_62 = 62 * 0.15 calculate_lung_60 = 60 * 0.15 calculate_lung_50 = 50 * 0.15 calculate_lung_40 = 40 * 0.15 calculate_lung_30 = 30 * 0.15 calculate_lung_20 = 20 * 0.15 calculate_lung_10 = 10 * 0.15
优化代码
# 肺活量评分,权重0.15 calculate_lung = {i: i * 0.15 for i in range(100, 9, -1)}
上述calculate_lung_10 等价于 calculate_lung[10]
最终实现一样的代码效果,获取对应的范围取值,直接减少了800行代码,成果满满。
标签:10,calculate,django,0.15,代码优化,lung,100,全局变量 From: https://www.cnblogs.com/weiweirui/p/17903983.html