【发布时间】:2018-04-15 16:06:54
【问题描述】:
在将一些 python 代码移植到 PHP 中时,我遇到了以下代码的问题:
def getOrAdd(self, config):
h = config.hashCodeForConfigSet()
l = self.configLookup.get(h, None)
if l is not None:
r = next((c for c in l if config.equalsForConfigSet(c)), None)
if r is not None:
return r
if l is None:
l = [config]
self.configLookup[h] = l
else:
l.append(config)
return config
我想不通,那一行是什么
r = next((c for c in l if config.equalsForConfigSet(c)), None)
确实是这个意思。
谁能解释一下这句话的含义?
提前谢谢你!
【问题讨论】:
-
在本例中,
next只是获取推导式的第一个值,如果为空,则为None。
标签: python generator next generator-expression