优化代码并避免python中的if语句

时间:2018-12-11 11:27:23

标签: python

我有一个带有几个if语句的代码,这些语句检查某些变量的组合范围。我的代码如下:

cnt1, cnt2, cnt3, cnt4, cnt5, cnt6, cnt7, cnt8, cnt9, \
                    cnt10, cnt11, cnt12 = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

var1_1, var1_2, var1_3 var2_1, var2_2, var2_3, var3_1, var3_2, var3_3, var4_1, var4_2, var4_3, \
var5_1, var5_3, var5_3, var6_1, var6_2, var6_3, var7_1, var7_2, var7_3, var8_1, var8_2, var8_3 \
var9_1, var9_2, var9_3, var10_1, var10_2, var10_3, var11_1, var11_2, var11_3, var12_1, var12_2, \
var12_3 = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

if case == 1:
  cnt1 = cnt1 + 1
  if common_edge < 0.33:
    var1_1 = var1_1 + common_var
  elif 0.33<= common_edge < 0.66:
    var1_2 = var1_2 + common_var
  elif common_edge>= 0.66:
    var1_3 = var1_3 + common_var

elif case == 2:
 cnt2 = cnt2 + 1
 if common_edge < 0.33:
    var2_1 = var2_1 + common_var
 elif 0.33<= common_edge < 0.66:
    var2_2 = var2_2 + common_var
 elif common_edge>= 0.66:
    var2_3 = var2_3 + common_var
elif case == 3:
 cnt3 = cnt3 + 1
 if common_edge < 0.33:
    var3_1 = var3_1 + common_var
 elif 0.33<= common_edge < 0.66:
    var3_2 = var3_2 + common_var
 elif common_edge>= 0.66:
    var3_3 = var3_3 + common_var
elif case == 4:
 cnt4 = cnt4 + 1
 if common_edge < 0.33:
    var4_1 = var4_1 + common_var
 elif 0.33<= common_edge < 0.66:
    var4_2 = var4_2 + common_var
 elif common_edge>= 0.66:
    var4_3 = var4_3 + common_var
elif case == 5:
 cnt5 = cnt5 + 1
 if common_edge < 0.33:
    var5_1 = var5_1 + common_var
 elif 0.33<= common_edge < 0.66:
    var5_2 = var5_2 + common_var
 elif common_edge>= 0.66:
    var5_3 = var5_3 + common_var
elif case == 6:
 cnt6 = cnt6 + 1
 if common_edge < 0.33:
    var6_1 = var6_1 + common_var
 elif 0.33<= common_edge < 0.66:
    var6_2 = var6_2 + common_var
 elif common_edge>= 0.66:
    var6_3 = var6_3 + common_var
elif case == 7:
 cnt7 = cnt7 + 1
 if common_edge < 0.33:
    var7_1 = var7_1 + common_var
 elif 0.33<= common_edge < 0.66:
    var7_2 = var7_2 + common_var
 elif common_edge>= 0.66:
    var7_3 = var7_3 + common_var
elif case == 8:
 cnt8 = cnt8 + 1
 if common_edge < 0.33:
    var8_1 = var8_1 + common_var
 elif 0.33<= common_edge < 0.66:
    var8_2 = var8_2 + common_var
 elif common_edge>= 0.66:
    var8_3 = var8_3 + common_var
elif case == 9:
 cnt9 = cnt9 + 1
 if common_edge < 0.33:
    var9_1 = var9_1 + common_var
 elif 0.33<= common_edge < 0.66:
    var9_2 = var9_2 + common_var
 elif common_edge>= 0.66:
    var9_3 = var9_3 + common_var
elif case == 10:
 cnt10 = cnt10 + 1
 if common_edge < 0.33:
    var10_1 = var10_1 + common_var
 elif 0.33<= common_edge < 0.66:
    var10_2 = var10_2 + common_var
 elif common_edge>= 0.66:
    var10_3 = var10_3 + common_var
elif case == 11:
 cnt11 = cnt11 + 1
 if common_edge < 0.33:
    var11_1 = var1_11 + common_var
 elif 0.33<= common_edge < 0.66:
    var11_2 = var11_2 + common_var
 elif common_edge>= 0.66:
    var11_3 = var11_3 + common_var
elif case == 12:
 cnt12 = cnt12 + 1
 if common_edge < 0.33:
    var12_1 = var12_1 + common_var
 elif 0.33<= common_edge < 0.66:
    var12_2 = var12_2 + common_var
 elif common_edge>= 0.66:
    var12_3 = var12_3 + common_var

如何优化该代码,并且不使用那么多的if语句。有没有一种避免if-else的平滑方法?

2 个答案:

答案 0 :(得分:4)

api success

答案 1 :(得分:1)

您可以尝试:

cnt = {item: 0 for item in range(12)}
var = {item: [0, 0, 0] for item in range(12)}


def you_function(case, cnt, common_edge, var, common_var):
    cnt[case] += 1
    if common_edge < 0.333:
        var[case][0] += common_var
    elif 0.33 <= common_edge < 0.66:
        var[case][1] += common_var
    elif common_edge >= 0.66:
        var[case][2] += common_var
    return cnt, var