我可以为封闭循环跳过不确定数量的步骤吗? (Python)

发布于 2024-09-13 05:55:22 字数 1180 浏览 6 评论 0原文

这也许是糟糕设计的结果,但事实就是如此。我不太确定如何解释这个问题。

所以我有迭代单词列表的代码。 (此列表不会更改。)然后,代码根据一组条件解析某些单词并将其组合在一起,并将它们存储在一个新列表中。主循环一次获取一个单词,然后需要跳过代码认为合适的部分。因此,例如:

主循环的单词列表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里南柯 2024-09-20 05:55:22

您可以显式使用迭代器的 next 方法。

>>> l = [1, 2, 3, 4, 5, 6]
>>> l_iter = iter(l)
>>> for n in l_iter:
      if n==2:
        print '{0}+{1}+{2}'.format(n, l_iter.next(), l_iter.next())
      else:
        print n
1
2+3+4
5
6

编辑:是的,与枚举结合时会变得混乱。我想到的另一个选择是:将函数编写为生成器,例如:

def combined_words(word_list):
  combined_word = ""
  for k, word in enumerate(word_list):
    combined_word += k
    if next_word_can_be_paired:
      pass
    else: # next word can't be paired
      yield combined_word
      combined_word = ""
  if combined_word:
    yield combined_word # just in case anything is left over

然后调用 list(combined_words(word_list)) 来获取新列表。

You could explicitly use the next method of the iterator.

>>> l = [1, 2, 3, 4, 5, 6]
>>> l_iter = iter(l)
>>> for n in l_iter:
      if n==2:
        print '{0}+{1}+{2}'.format(n, l_iter.next(), l_iter.next())
      else:
        print n
1
2+3+4
5
6

EDIT: yeah, that would get messy when combined with enumerate. Another option that comes to mind: write the function as a generator, something like:

def combined_words(word_list):
  combined_word = ""
  for k, word in enumerate(word_list):
    combined_word += k
    if next_word_can_be_paired:
      pass
    else: # next word can't be paired
      yield combined_word
      combined_word = ""
  if combined_word:
    yield combined_word # just in case anything is left over

Then call list(combined_words(word_list)) to get the new list.

我不在是我 2024-09-20 05:55:22

您可以尝试使用 itertools 模块,尤其是 dropwhile 函数。

you could try to use the itertools module and espacially the dropwhile function.

吻泪 2024-09-20 05:55:22

我错过了什么吗?

word_list = ["apple", "banana", "penguin"]
skip_list = {}

for word in self.word_list:
    if word in skip_list:
        continue

    # Do word-pairing logic; if word paired, skip_list[word] = 1

可能不是最高效的编程方式,但至少清晰简洁。

Am I missing something?

word_list = ["apple", "banana", "penguin"]
skip_list = {}

for word in self.word_list:
    if word in skip_list:
        continue

    # Do word-pairing logic; if word paired, skip_list[word] = 1

May not be the most programmatically efficient, but at least clear and concise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文