Python: 如何终止无限循环?
Python: How to kill a infinite for loop?
我有一个 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 时我看到了打印消息,但它一直在继续。
有什么想法吗?
您需要在 loop.that 限制循环继续之前检查长度。
如果 len(keywords)
永远不会 == 100
.
怎么办
因为keywords.append(k)
在循环中。
示例:len(keywords) == 98
循环后 len(keywords)
变为 103
.
尝试更改 if
语句以测试 len(keywords)
是否大于或等于 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
这是因为你有 2 个循环,所以你必须中断两次,我不认为这是最优化的解决方案,但它可以完成工作:
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)
您应该避免修改正在循环访问的列表,因为这会导致奇怪的行为。最好有一个尚未尝试过的查询列表,你可以边走边用尽和填充,以及一个单独的循环(for i in range(100)
或带有中断条件的 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(keywords)
可能会从 <100
变为 >100
而不会达到条件。然后它永远不会,你又会得到一个无限循环。
相反,它应该是 len(keywords) >= 100
,尽管这样你会遇到 keywords
不完全是 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)
更好的方法是使用第二个列表并使用 extend() 函数
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])
我有一个 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 时我看到了打印消息,但它一直在继续。
有什么想法吗?
您需要在 loop.that 限制循环继续之前检查长度。
如果 len(keywords)
永远不会 == 100
.
怎么办
因为keywords.append(k)
在循环中。
示例:len(keywords) == 98
循环后 len(keywords)
变为 103
.
尝试更改 if
语句以测试 len(keywords)
是否大于或等于 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
这是因为你有 2 个循环,所以你必须中断两次,我不认为这是最优化的解决方案,但它可以完成工作:
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)
您应该避免修改正在循环访问的列表,因为这会导致奇怪的行为。最好有一个尚未尝试过的查询列表,你可以边走边用尽和填充,以及一个单独的循环(for i in range(100)
或带有中断条件的 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(keywords)
可能会从 <100
变为 >100
而不会达到条件。然后它永远不会,你又会得到一个无限循环。
相反,它应该是 len(keywords) >= 100
,尽管这样你会遇到 keywords
不完全是 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)
更好的方法是使用第二个列表并使用 extend() 函数
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])