1
for s in strategies:
        strats_having_fcs = {a.strategy: a.algorithmType for a in s.algorithms if a.algorithmType == AlgorithmTypeEnum.feedback_control.value}

我们可以通过理解把它写成单行吗?

4

2 回答 2

2

只是:

strats_having_fcs = {a.strategy: a.algorithmType for s in strategies for a in s.algorithms if a.algorithmType == AlgorithmTypeEnum.feedback_control.value}

看看这个关于列表内理解的 SO 问题。对我来说,这清楚地说明了这背后的逻辑是如何运作的。

于 2019-01-31T00:42:45.497 回答
1

IIUC,也许您正在寻找这种嵌套理解

strats_having_fcs = {a.strategy: a.algorithmType for s in strategies for a in s.algorithms 
                     if a.algorithmType == AlgorithmTypeEnum.feedback_control.value}
于 2019-01-31T00:41:46.397 回答