당신은 조건부 발전기 표현에 default=False
와 next
을 사용할 수
next((string for string in myStringArray if string in comment.body), default=False)
일치하는 어떤 항목이없는 경우 기본값은 반환된다 (그래서 False
를 반환 any
같다), 그렇지 않으면 첫 번째 일치 항목이 반환된다 .
이것은 거의 비슷하다 :
isMatch = False # variable to store the result
for string in myStringArray:
if string in comment.body:
isMatch = string
break # after the first occurrence stop the for-loop.
또는 다른 변수에 isMatch
및 whatMatched
을 갖고 싶어 :
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.
: 위의 대답으로, 한 성명에서이 작업을 수행하는 독창적 인 방법이 있지만, 그것은 정말
for
루프를 사용하는 것이 합리적이다 'any'를 사용하고 명시적인 for 루프를 사용하여 검사를 수행하십시오. 여기에 어떤 문제도 보이지 않는다. –