我可以为封闭循环跳过不确定数量的步骤吗? (Python)
这也许是糟糕设计的结果,但事实就是如此。我不太确定如何解释这个问题。
所以我有迭代单词列表的代码。 (此列表不会更改。)然后,代码根据一组条件解析某些单词并将其组合在一起,并将它们存储在一个新列表中。主循环一次获取一个单词,然后需要跳过代码认为合适的部分。因此,例如:
主循环的单词列表:
ListA = [苹果,香蕉,企鹅]
在主循环中,假设我的代码决定苹果和香蕉属于一起,所以
ListB = [苹果香蕉,企鹅]
现在我想要主循环跳过香蕉,它不需要运行检查是否香蕉是否与其他东西配对。所以我会使用 continue 语句。问题就在这里。我不知道有多少个单词最终会配对。所以我最终可能需要一个继续,或者三个继续。我能想到的根据需要多次运行 continue 的唯一方法是使用循环......但这会产生问题,因为 continue 会影响它所在的循环。
有没有办法让主循环根据需要继续执行多次?也许我错过了一些简单的事情。感谢您的帮助。
编辑
word_list = ["apple", "banana", "penguin"] #word_list will be arbitrary in practice
phrase_length = 0 #phrase_length is the amount of times I would like to skip
for k, word in enumerate(word_list):
#I would like to have the continues run here, before any of the below code
#the code down here decides what to pair in a forward fashion
#so it starts from apple and looks ahead to see if something fits with it
#continues in this manner till it comes up with the longest possible pairing
#phrase_length is then set equal to the amount of words used make the pairing
如果它还必须执行香蕉的代码,并从那里向前检查,则会浪费大量的计算时间。这就是为什么我想跳过香蕉检查。
This is perhaps a result of bad design, but here it goes. I wasn't quite sure how to explain this problem.
So I have code that iterates over a list of words. (This list does not change.) The code then parses and combines certain words together, depending on a set of criteria, storing them in a new list. The master loop, which is taking each word one at a time, would then need to skip what the code decided was a fit. So for example:
Master loop's list of words:
ListA = [apple, banana, penguin]
Within the master loop, let's say my code decided apple and bananna belong together, so
ListB = [apple banana, penguin]
Now I would like to have Master Loop skip over bananna, it doesn't need to run the check over whether to see if banana pairs with something else. So I would use a continue statement. Here is the problem. I don't know how many words will end up paired. So I could end up needing one continue, or three continues. The only way I can think of to run continue as many times as needed would be to use a loop...but that creates a problem since continue would affect the loop it is within.
Is there a way for me to make the master loop continue as many times as needed? Perhaps I am missing something simple. Thanks for your help.
EDIT
word_list = ["apple", "banana", "penguin"] #word_list will be arbitrary in practice
phrase_length = 0 #phrase_length is the amount of times I would like to skip
for k, word in enumerate(word_list):
#I would like to have the continues run here, before any of the below code
#the code down here decides what to pair in a forward fashion
#so it starts from apple and looks ahead to see if something fits with it
#continues in this manner till it comes up with the longest possible pairing
#phrase_length is then set equal to the amount of words used make the pairing
It would waste a considerable amount of computing time, if it had to execute the code for banana as well, checking forward from there as well. Which is why I would want to skip over the banana check.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以显式使用迭代器的
next
方法。编辑:是的,与枚举结合时会变得混乱。我想到的另一个选择是:将函数编写为生成器,例如:
然后调用 list(combined_words(word_list)) 来获取新列表。
You could explicitly use the
next
method of the iterator.EDIT: yeah, that would get messy when combined with enumerate. Another option that comes to mind: write the function as a generator, something like:
Then call
list(combined_words(word_list))
to get the new list.您可以尝试使用 itertools 模块,尤其是 dropwhile 函数。
you could try to use the itertools module and espacially the dropwhile function.
我错过了什么吗?
可能不是最高效的编程方式,但至少清晰简洁。
Am I missing something?
May not be the most programmatically efficient, but at least clear and concise.