下面的代码基于this recipe。然而,配方的关键点——如果迭代器为空,它提供了在迭代器上中断迭代的方法——在这里似乎不起作用,而是以以下不希望的方式运行:
我如何确保:
代码如下:
def my_func(choice_pattern, input):
# Search in input for some things to choose from.
choice_iterator = choice_pattern.finditer(input, re.M)
if not choice_iterator:
print "No choices. Exiting..."
sys.exit()
else:
# Show choices to the user. For each one, ask user for a yes/no response. If
# choice accepted, return a result. Otherwise show user next choice. If no
# choices accepted by user, quit.
for choice in choice_iterator:
print choice.group()
# get_yes_no_answer() returns True or False depending on user response.
if get_yes_no_answer():
return choice
else:
# Check if iterator is empty. If so, quit; if not, do something else.
try:
next_choice = choice_iterator.next()
except StopIteration:
print "No matches. Exiting..."
sys.exit()
else:
choice_iterator = itertools.chain([next_choice], choice_iterator)
您不需要检查迭代器是否为空。for循环将为您执行此操作,并在迭代器为空时停止。就这么简单。
另外,在sys.exit()或返回之后不需要
else
。完成后,您的代码如下所示:
就这样!
结果是你在循环中,也会得到下一个项目。结果是你事实上只显示每一秒的答案。
事实上,您可以简化它:
迭代器几乎和任何序列类型一样使用。你不需要把它和单子区别对待。
请参阅this question中的成对迭代器。然后您可以检查最后一个项目,如:
在你展示的例子中,这似乎没有必要。您不需要检查finditer是否返回了迭代器,因为它总是返回迭代器。如果你找不到你想要的东西,你就可以掉入for循环:
你为什么这么做?为什么不:
我不知道你输入的长度是多少,但我怀疑这样做值得。
相关问题 更多 >
编程相关推荐