我有两个 NumPy 数组,比如 num 和 denom。我需要根据各个元素是否为零以num和denom返回特定值,
r2 = []
for denind, denel in enumerate(denom):
numel = num[denind]
if denel: # Denominator is not zero
r2.append(1 - numel/denom)
elif numel: # Denominator is zero, but numerator is not zero
r2.append(0.0)
else: # Both denominator and numerator are zero.
r2.append(1.0)
return np.array(r2)
是否有一种“NumPy”方式来进行这样的迭代。