next((string for string in myStringArray if string in comment.body), default=False)
如果没有匹配的项,则返回默认值(因此类似于any返回{}),否则返回第一个匹配项。在
这大致相当于:
^{pr2}$
或者,如果您希望isMatch和{}在不同的变量中:
isMatch = False # variable to store the any result
whatMatched = '' # variable to store the first match
for string in myStringArray:
if string in comment.body:
isMatch = True
whatMatched = string
break # after the first occurrence stop the for-loop.
我同意这样的评论,即显式循环将是最清晰的。你可以这样敷衍你原来的样子:
其中:
^{pr2}$如果}是{},则保留它最初拥有的任何值(如果有)。在
isMatch
是True
,则全局memory
将包含匹配的字符串,或者如果{使用一个变量来存储两种不同类型的信息不是一个好主意:一个字符串是否匹配(a
bool
),以及这个字符串是什么(astring
)。在您实际上只需要第二条信息:虽然在一个语句中有创造性的方法来实现这一点,如上面的答案所示,使用
for
循环确实有意义:可以对条件生成器表达式使用^{} 和
default=False
一起使用:如果没有匹配的项,则返回默认值(因此类似于}),否则返回第一个匹配项。在
any
返回{这大致相当于:
^{pr2}$或者,如果您希望}在不同的变量中:
isMatch
和{相关问题 更多 >
编程相关推荐