需要使用理解将代码包装成单行

2024-06-16 11:45:39 发布

您现在位置:Python中文网/ 问答频道 /正文

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

通过理解我们能把它写成一行吗


Tags: inforifvaluecontrolfeedbackstrategiesfcs
2条回答

IIUC,也许你在寻找这个嵌套的理解

strats_having_fcs = {a.strategy: a.algorithmType for s in strategies 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}

看一下this关于列表理解的问题。对我来说,这澄清了很多这背后的逻辑是如何运作的

相关问题 更多 >