【发布时间】:2018-11-06 11:48:52
【问题描述】:
所以我正在使用通过以下方式向系统添加组件的功能:
es.add(components[i][0] for i in components.keys())
components 是一个 python 字典,看起来像这样:
components = {'a': (a0, a1),
'b': (b0, None)}
我想要实现的是我想运行上面提到的for-loop 和except,如果components 字典给出None,它将无法运行add() 函数。
我尝试了什么:
es.add(components[i][1] for i in components.keys() except None)
Ofc 它给出了一个语法错误。它的语法是什么?
示例:
es.add(components[i][1] for i in components.keys())
以上等于:
es.add(a1)
es.add(None)
我想以一种只添加 a1 并跳过 None 的方式编写我的 for 循环。
【问题讨论】:
-
你是什么意思,运行它“期望”?在这种情况下
expect是什么? -
如果字典的输入不是
None,则运行add()函数。 -
也许他的意思是除了?
-
你是对的,固定问题
标签: python dictionary for-loop syntax