【发布时间】:2017-04-30 22:50:49
【问题描述】:
我在这个答案https://stackoverflow.com/a/37401376/1727657 中找到了一些我需要的代码。但我不明白next() 在这种情况下做了什么。谁能解释一下?
这是我为理解它而制作的一个简短的测试脚本。我们的想法是查看测试字符串txt 是否包含myset 中的任何字符串,如果是,是哪一个。它有效,但我不知道为什么。
myset = ['one', 'two', 'three']
txt = 'A two dog night'
match = next((x for x in myset if x in txt), False)
if match: #if match is true (meaning something was found)
print match #then print what was found
else:
print "not found"
我的下一个问题是问next() 是否会给我match 的索引(或者我是否需要在txt 上做find())?
【问题讨论】:
-
我猜他们在这里使用
next,因为他们只想要第一场比赛。该行可能只是match = [x for x in myset if x in txt]以获取所有匹配项。
标签: python python-2.7 next