当多个语句受到影响时,如何向量化if语句?

我想要矢量化我的python代码。

如果只有一条语句应该在之后执行,我能够以一种优雅的方式替换简单的if语句:

if a < b:
    c = 5

获取:

c = np.where(a<b,5,c)

如果if语句后面有很多语句,有没有一种很好的矢量化方法?

if a y b:
    c = 5
    d = 6
    e = 7
    f = 8
    .... 
    z = 99

我想避免有很多类似的(某种程度上不是somehow的)语句,比如:

c = np.where(a<b,5,c)
d = np.where(a<b,6,c)
e = np.where(a<b,7,c)
f = np.where(a<b,8,c)
.... 
z = np.where(a<b,99,c)

似乎不可能对np.where使用元组,还是我错了?

转载请注明出处:http://www.intsu.net/article/20230509/1084788.html