有没有办法在 Python 中组合多个列表理解?
Is there a way to combine multiple list comprehensions in Python?
我的代码中有一个函数:
def get_weights(Last_scan, Peak_shot):
Est_Succshots = [x*Peak_shot for x in Last_scan.values()]
Est_error = [np.sqrt(i)/Peak_shot for i in Est_Succshots]
Is = [1/m for m in Est_error]
Weights = [i/sum(Is) for i in Is]
return Weights
我使用了 4 个列表理解来进行计算。我想知道是否有更短的方法或不同的方法可以执行此类计算?感谢您的帮助:)
假设Last_scan.values()
是一个数字列表,Peak_shot
是一个数字,这似乎等同于
def get_weights(Last_scan, Peak_shot):
Is = np.sqrt(Peak_shot/np.array(Last_scan.values()))
return Is/Is.sum()
我的代码中有一个函数:
def get_weights(Last_scan, Peak_shot):
Est_Succshots = [x*Peak_shot for x in Last_scan.values()]
Est_error = [np.sqrt(i)/Peak_shot for i in Est_Succshots]
Is = [1/m for m in Est_error]
Weights = [i/sum(Is) for i in Is]
return Weights
我使用了 4 个列表理解来进行计算。我想知道是否有更短的方法或不同的方法可以执行此类计算?感谢您的帮助:)
假设Last_scan.values()
是一个数字列表,Peak_shot
是一个数字,这似乎等同于
def get_weights(Last_scan, Peak_shot):
Is = np.sqrt(Peak_shot/np.array(Last_scan.values()))
return Is/Is.sum()