我正在寻找一个执行以下搜索的单行Python表达式:
targets = ['habble', 'norpouf', 'blantom']
entry = "Sorphie porre blantom nushblot"
found=False
for t in targets :
if t in entry :
found = True
break
print ("found" if found else "not found")
Run Code Online (Sandbox Code Playgroud)
这样我们就可以写的东西像这样:
print("found" if entry.contains(targets) else "not found")
Run Code Online (Sandbox Code Playgroud)
你可以使用any:
targets = ['habble', 'norpouf', 'blantom']
entry = "Sorphie porre blantom nushblot"
result = 'found' if any(i in entry for i in targets) else 'not found'
Run Code Online (Sandbox Code Playgroud)