Python:如何杀死无限for循环?

Python:如何杀死无限for循环?,python,for-loop,logic,Python,For Loop,Logic,我有一个for循环,它进入一个包含10个项目的列表。对于它拥有的每一项,它都会使用For循环在列表中再添加10项。我想在列表长度为100时终止循环,这是可能的还是我无法退出循环,因为列表永远不会停止增长 这是我的代码: keywords = [ "apple store", "applesauce recipe", "applesauce", "appleseeds", "apples to apples&

我有一个for循环,它进入一个包含10个项目的列表。对于它拥有的每一项,它都会使用For循环在列表中再添加10项。我想在列表长度为100时终止循环,这是可能的还是我无法退出循环,因为列表永远不会停止增长

这是我的代码:

keywords = [
"apple store",
"applesauce recipe",
"applesauce",
"appleseeds",
"apples to apples",
"apples and honey",
"applesauce cake",
"apple support",
"apples and bananas"
]

   for i in keywords:
            url = "http://suggestqueries.google.com/complete/search?output=firefox&q=" + i
            response = requests.get(url, verify=False)
            suggestions = json.loads(response.text)
            for word in suggestions[1]:
                k = word
                keywords.append(k)
                print(word)

我尝试在结尾添加以下内容,但没有成功


if len(keywords) == 100:
    print('this is where it needs to break')
    break


我看到打印消息时,它达到100,但它继续


有什么想法吗?

你需要检查循环前的长度。这将限制循环继续。

如果
len(关键字)
从来都不是
==100

因为
关键字.append(k)
处于循环中。
示例:
len(关键字)==98
循环后
len(关键字)
变成
103

尝试更改
if
语句,以测试
len(关键字)
是否大于或等于
100

for i in keywords:
    url = "http://suggestqueries.google.com/complete/search?output=firefox&q=" + i
    response = requests.get(url, verify=False)
    suggestions = json.loads(response.text)
    for word in suggestions[1]:
        keywords.append(word)
        print(word)

    if len(keywords) >= 100:  # test if greater or equal than 100
        print('this is where it needs to break')
        break

这是因为两个循环相互之间有两个循环,所以必须断开两次,我认为这不是最优化的解决方案,但它确实起到了作用:

import requests
import json

keywords = [
"apple store",
"applesauce recipe",
"applesauce",
"appleseeds",
"apples to apples",
"apples and honey",
"applesauce cake",
"apple support",
"apples and bananas"
]

for i in keywords:
    url = "http://suggestqueries.google.com/complete/search?output=firefox&q=" + i
    response = requests.get(url, verify=False)
    suggestions = json.loads(response.text)
    if len(keywords) == 100:
        print('this is where it needs to break')
        break
    else:
        for word in suggestions[1]:
            if len(keywords) == 100:
                print('this is where it needs to break')
                break
            else:
                keywords.append(word)
                print(word)

您应该避免修改正在迭代的列表,因为它可能导致奇怪的行为。最好有一个未被尝试过的查询列表,您可以在运行时耗尽并填充这些查询,还有一个单独的循环(对于范围(100)中的i,可以是
,也可以是带有中断条件的while循环)

对于您显示的代码,有两个选项,这取决于您放置中断条件的位置

第1版: 也许你在做什么

for i in keywords:
    url = "http://suggestqueries.google.com/complete/search?output=firefox&q=" + i
    response = requests.get(url, verify=False)
    suggestions = json.loads(response.text)
    for word in suggestions[1]:
        k = word
        keywords.append(k)
        print(word)

        if len(keywords) == 100:
            print('this is where it needs to break')
            break
在这里,你只打破了内部循环。外部循环一直在进行,所以你有一个无限循环,因为它不断撞击内部循环,然后一次又一次地爆发,因为外部循环永远不会终止

第2版: 仍然可以很容易地产生一个无限循环

for i in keywords:
    url = "http://suggestqueries.google.com/complete/search?output=firefox&q=" + i
    response = requests.get(url, verify=False)
    suggestions = json.loads(response.text)
    for word in suggestions[1]:
        k = word
        keywords.append(k)
        print(word)

    if len(keywords) == 100:
        print('this is where it needs to break')
        break
在这里,外部循环将结束。但是,它可能不会,因为中断条件的表述很糟糕。由于可以在每个内部循环中添加多个单词,
len(关键字)
可能会从
100
开始,而不会碰到条件。然后它永远不会,你会再次得到一个无限循环。 相反,它应该是
len(keywords)>=100
,尽管在这种情况下
关键字不完全是100


但真正的答案是,你不应该首先修改你正在迭代的列表,然后相应地重新构造你的逻辑。

通常在遍历列表时修改列表被认为是不好的做法。尽管如此,这是一种方法:

keywords = [
"apple store",
"applesauce recipe",
"applesauce",
"appleseeds",
"apples to apples",
"apples and honey",
"applesauce cake",
"apple support",
"apples and bananas"
]

for i in keywords:
    url = "http://suggestqueries.google.com/complete/search?output=firefox&q=" + i
    response = requests.get(url, verify=False)
    suggestions = json.loads(response.text)
    if len(keywords) >= 100:
        print('this is where it needs to break')
        break
    for word in suggestions[1]:
        k = word
        keywords.append(k) if len(keywords) < 100 else 0 # you can put basically anything here insetad of 0
        print(word)


我认为你在关键词后面加上了你正在循环的关键词列表,这个列表可能在不断增长。尝试在每次迭代结束时打印
len(关键字)
。此外,不需要将
word
设置为
k
,只需添加
word
是的,我添加了打印长度(关键字)并不断增长。我想在len为100时杀死它。
break
终止最近的包装范围,也许你终止了内循环,但外循环仍在循环?哦,明白了!我需要将if-else移动到外循环!现在它起作用了。谢谢当我设置len(关键字)==1000时,SSSIM看到了一些问题。出于某种原因,它达到了1000,但它还在继续。你能举个例子吗?我不能很好地理解它。我希望我的列表只增长到100个,那么添加一个条件,在其中循环I如何。用len(kewords)添加“AND”。应该有用的,明白了。现在它有意义了。我当时已经打了100分了,但它还在继续。现在>=停在104或接近104。非常感谢
keywords = [
"apple store",
"applesauce recipe",
"applesauce",
"appleseeds",
"apples to apples",
"apples and honey",
"applesauce cake",
"apple support",
"apples and bananas"
]

new_keywords = list(keywords) # storing a copy of keywords in new_keywords

for i in keywords:
    url = "http://suggestqueries.google.com/complete/search?output=firefox&q=" + i
    response = requests.get(url, verify=False)
    suggestions = json.loads(response.text)
    
    new_keywords.extend(suggestions[1])