python 中 while 循环的最直接替代方案

发布于 2024-10-18 09:03:25 字数 784 浏览 5 评论 0原文

该程序的主要目的是搜索单词并确保它们以正确的顺序出现。

我目前正在使用代码

while not corpus[index_F2-1] == corpus[index_B]:
    index_F2 = corpus.index(corpus[index_F2], index_F2+1) 

来搜索单词,检查以确保给定单词出现在其前面,如果该单词未出现在正确的环境中,则在语料库中查找同一单词的不同实例。此代码的唯一问题是,如果条件从未满足,它将返回错误。与其返回错误,不如退出循环,然后为“F2”分配一个新值以便重试。让程序退出循环的明显方法是使用 while 循环,但我不确定如何让 while 循环在当前循环有效的情况下不为 F2 找到新值。简而言之,如果脚本按照这个顺序运行,效果会最好。

  1. 检查当前的 F2 是否会起作用。
  2. 如果不起作用,请为 F2 分配一个新值。
  3. 再次检查递归并检查新的 F2 是否可以工作
  4. ,等等

if 语句可能是有序的,但我不确定它到底在哪里以及如何工作。另外,重新分配 F2 的代码已经存在。 未来可能出现的问题是

index_E2= corpus.index(corpus[index_E2], index_E2+1)

index_F = index_E2+1

index_F2 = corpus.index(corpus[index_F], index_F+1)

,语料库可能会用完 F2 值(由于用完 E2 值)并显示类似的错误,但这是另一个问题。

The general point of the program is to search for words and make sure that they occur in the right order.

I am currently using the code

while not corpus[index_F2-1] == corpus[index_B]:
    index_F2 = corpus.index(corpus[index_F2], index_F2+1) 

in order to search for a word, check to make sure that a given word appears before it, and find a different instance of the same word in the corpus if the word does not appear in the correct environment. The only problem with this code is that, if the conditions are never met, it returns an error. Rather than having it return an error, it would be better if it could exit the loop, and then assign a new value to "F2" in order to try again. The obvious way to make the program exit the loop would be to use a while loop, but I am unsure how to get a while loop to not find a new value for F2 if the current one works. In short, it would work best if the script ran in this order.

  1. Check to see if the current F2 will ever work
  2. If it will not work, assign a new value to F2.
  3. Check again recurse and check to see if the new F2 will work
  4. and so on

An if statement would probably be in order, but exactly where and how it would work, I am unsure. Also, the code for reassigning F2 already exists. It is

index_E2= corpus.index(corpus[index_E2], index_E2+1)

index_F = index_E2+1

index_F2 = corpus.index(corpus[index_F], index_F+1)

A future problem that could present itself is that the corpus may run out of F2 values ( as a result of running out of E2 values) and display a similar error, but that is another issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

旧夏天 2024-10-25 09:03:25

我想我可能明白你在寻找什么。听起来您想要有多个嵌套循环,并在最内层循环内有一个退出点。在我看来,实现此目的最明显的方法是将循环包含在函数中。由于所有这些 corpus 内容都很令人困惑,我将使用一个简单的示例:

words_to_find = ['tree', 'cow', 'lots_of_other_words']
words_to_search = ['bathtub', 'more_words']
def find_words(words_to_search, words_to_find):
    for i, fword in enumerate(words_to_find):
        for j, sword in enumerate(words_to_search):
            if fword == sword: 
                return (fword, i, j)

您可以在此处嵌套任意多个循环 - forwhile,无论如何——一旦你找到了你想要的东西,仍然可以轻松地摆脱所有这些。

如果这不能回答您的问题,请告诉我。


编辑:我想如果你想在一个 while 循环中完成这一切,你可以这样做:

word_list = ['blah', 'blah2', 'etc']
word_list2 = ['other', 'words']
i = 0
j = 0
while True:
    if i == len(word_list):
        i = 0
        j += 1
    elif thing_I_want(word_list[i], word_list2[j]):
        do_something()
        break
    else:
        i += 1

I think I might understand what you're looking for. It sounds like you want to have multiple nested loops, with an exit point within the innermost loop. The most obvious way to achieve this, in my mind, is to enclose the loops within a function. Since all this corpus stuff is confusing, I'll use a simple example:

words_to_find = ['tree', 'cow', 'lots_of_other_words']
words_to_search = ['bathtub', 'more_words']
def find_words(words_to_search, words_to_find):
    for i, fword in enumerate(words_to_find):
        for j, sword in enumerate(words_to_search):
            if fword == sword: 
                return (fword, i, j)

You can nest as many loops as you like here -- for, while, whatever -- and still break out of all of them easily once you've found what you want.

Let me know if that doesn't answer your question.


EDIT: I suppose if you wanted to have it all in one while loop you could do this:

word_list = ['blah', 'blah2', 'etc']
word_list2 = ['other', 'words']
i = 0
j = 0
while True:
    if i == len(word_list):
        i = 0
        j += 1
    elif thing_I_want(word_list[i], word_list2[j]):
        do_something()
        break
    else:
        i += 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文