Python 需要使用理解将代码包装成单行
我们能通过理解把它写成一行吗?IIUC,也许你在寻找这种嵌套的理解Python 需要使用理解将代码包装成单行,python,dictionary-comprehension,Python,Dictionary Comprehension,我们能通过理解把它写成一行吗?IIUC,也许你在寻找这种嵌套的理解 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}
只是:
看一看关于清单理解的问题。对我来说,这澄清了很多这背后的逻辑是如何工作的。是的,但它会给出一个接近s的错误。算法说s是未解决的引用。为什么你每次迭代都要反复强调strats的值?
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}