妥协Python if语句

时间:2015-06-29 11:33:18

标签: python if-statement

我必须在一个代码中使用一堆if语句。他们都是一样的,略有变化。我有什么方法可以妥协所有这些代码并使其更优雅和更短?

以下代码:

if con_name == 'coh':
    coh = my_coherence(n_freqs, Rxy_mean, Rxx_mean, Ryy_mean)
    coh_surro = my_coherence(n_freqs, Rxy_s_mean, Rxx_s_mean, Ryy_s_mean)
    return coh, coh_surro, freqs, freqs_surro

if con_name == 'imcoh':
    imcoh = my_imcoh(n_freqs, Rxy_mean, Rxx_mean, Ryy_mean)
    imcoh_surro = my_imcoh(n_freqs, Rxy_s_mean, Rxx_s_mean, Ryy_s_mean)
    return imcoh, imcoh_surro, freqs, freqs_surro

if con_name == 'cohy':
    cohy = my_cohy(n_freqs, Rxy_mean, Rxx_mean, Ryy_mean)
    cohy_surro = my_cohy(n_freqs, Rxy_s_mean, Rxx_s_mean, Ryy_s_mean)
    return cohy, cohy_surro, freqs, freqs_surro

if con_name == 'plv':
    plv = my_plv(n_freqs, Rxy, Rxy_mean)
    plv_surro = my_plv(n_freqs, Rxy_s, Rxy_s_mean)
    return plv, plv_surro, freqs, freqs_surro

if con_name == 'pli':
    pli = my_pli(n_freqs, Rxy, Rxy_mean)
    pli_surro = my_pli(n_freqs, Rxy_s, Rxy_s_mean)
    return pli, pli_surro, freqs, freqs_surro

if con_name == 'wpli':
    wpli = my_wpli(n_freqs, Rxy, Rxy_mean)
    wpli_surro = my_wpli(n_freqs, Rxy_s, Rxy_s_mean)
    return wpli, wpli_surro, freqs, freqs_surro

如果这很容易,我很抱歉,但我尝试过并试图找不到办法。

4 个答案:

答案 0 :(得分:5)

没有反思

func, flag = {
    "coh": (my_coherence, True),
    "imcoh": (my_imcoh, True)
    "cohy": (my_cohy, True),
    "ply": (my_plv, False),
    "pli": (my_pli, False),
    "wpli": (my_wpli, False)
}[con_name]

args = (n_freqs, Rxy_mean, Rxx_mean, Ryy_mean) if flag else (n_freqs, Rxy, Rxy_mean)
surro_args = (n_freqs, Rxy_s_mean, Rxx_s_mean, Ryy_s_mean) if flag else (n_freqs, Rxy_s Rxy_s_mean)

val = func(*args)
surro = func(*surro_args)
return val, surro, freqs, freqs_surro

或者这也是可能的

...
args = (Rxy_mean, Rxx_mean, Ryy_mean) if flag else (Rxy, Rxy_mean)
surro_args = (Rxy_s_mean, Rxx_s_mean, Ryy_s_mean) if flag else (Rxy_s Rxy_s_mean)

val = func(n_freqs, *args)
surro = func(n_freqs *surro_args)
...

也许flag有一个很酷的名字来分类这些功能。请改用它。

答案 1 :(得分:2)

我会用字典。类似的东西:

functions = {'coh':my_coherence,'imcoh':my_imcoh....}

这将被称为:

functions[con_name](n_freqs, Rxy_s_mean, Rxx_s_mean, Ryy_s_mean)

答案 2 :(得分:1)

r = {
    'coh':   lambda: (my_coherence(n_freqs, Rxy_mean, Rxx_mean, Ryy_mean), my_coherence(n_freqs, Rxy_s_mean, Rxx_s_mean, Ryy_s_mean), freqs, freqs_surro),
    'imcoh': lambda: (my_imcoh(n_freqs, Rxy_mean, Rxx_mean, Ryy_mean), my_imcoh(n_freqs, Rxy_s_mean, Rxx_s_mean, Ryy_s_mean), freqs, freqs_surro),
    'cohy':  lambda: (my_cohy(n_freqs, Rxy_mean, Rxx_mean, Ryy_mean), my_cohy(n_freqs, Rxy_s_mean, Rxx_s_mean, Ryy_s_mean), freqs, freqs_surro),
    'plv':   lambda: (my_plv(n_freqs, Rxy, Rxy_mean), my_plv(n_freqs, Rxy_s, Rxy_s_mean), freqs, freqs_surro),
    'pli':   lambda: (my_pli(n_freqs, Rxy, Rxy_mean), my_pli(n_freqs, Rxy_s, Rxy_s_mean), freqs, freqs_surro),
    'wpli':  lambda: (my_wpli(n_freqs, Rxy, Rxy_mean), my_wpli(n_freqs, Rxy_s, Rxy_s_mean), freqs, freqs_surro),
    }
return r[con_name]()

您可以进一步避免重复,例如,对每种情况重复最后两项:

r = {
    'coh':   lambda: (my_coherence(n_freqs, Rxy_mean, Rxx_mean, Ryy_mean), my_coherence(n_freqs, Rxy_s_mean, Rxx_s_mean, Ryy_s_mean)),
    'imcoh': lambda: (my_imcoh(n_freqs, Rxy_mean, Rxx_mean, Ryy_mean), my_imcoh(n_freqs, Rxy_s_mean, Rxx_s_mean, Ryy_s_mean)),
    'cohy':  lambda: (my_cohy(n_freqs, Rxy_mean, Rxx_mean, Ryy_mean), my_cohy(n_freqs, Rxy_s_mean, Rxx_s_mean, Ryy_s_mean)),
    'plv':   lambda: (my_plv(n_freqs, Rxy, Rxy_mean), my_plv(n_freqs, Rxy_s, Rxy_s_mean)),
    'pli':   lambda: (my_pli(n_freqs, Rxy, Rxy_mean), my_pli(n_freqs, Rxy_s, Rxy_s_mean)),
    'wpli':  lambda: (my_wpli(n_freqs, Rxy, Rxy_mean), my_wpli(n_freqs, Rxy_s, Rxy_s_mean)),
    }
return r[con_name]() + (freqs, freqs_surro)

答案 3 :(得分:0)

您可以使用Reflection。我会创建一个方法,负责调用给定的方法。我会在每种情况下传递两个方法名称和参数。根据方法param名称,您可以每次调用正确的函数并返回所需的内容。在你展示的代码块中,我将调用该方法,传递两个方法名称和参数。