for s in strategies:
strats_having_fcs = {a.strategy: a.algorithmType for a in s.algorithms if a.algorithmType == AlgorithmTypeEnum.feedback_control.value}
我们可以通过理解把它写成单行吗?
for s in strategies:
strats_having_fcs = {a.strategy: a.algorithmType for a in s.algorithms if a.algorithmType == AlgorithmTypeEnum.feedback_control.value}
我们可以通过理解把它写成单行吗?
只是:
strats_having_fcs = {a.strategy: a.algorithmType for s in strategies for a in s.algorithms if a.algorithmType == AlgorithmTypeEnum.feedback_control.value}
看看这个关于列表内理解的 SO 问题。对我来说,这清楚地说明了这背后的逻辑是如何运作的。
IIUC,也许您正在寻找这种嵌套理解
strats_having_fcs = {a.strategy: a.algorithmType for s in strategies for a in s.algorithms
if a.algorithmType == AlgorithmTypeEnum.feedback_control.value}