Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 需要使用理解将代码包装成单行_Python_Dictionary Comprehension - Fatal编程技术网

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

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} 只是: 看一看关于清单理解的问题。对我来说,这澄清了很多这背后的逻辑是如何工作的。是的,但它会给出一个

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

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

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}