[英]How to iterate and loop through every array value in python
while True:
for i in range(len(listing_ids)):
response = requests.get(f'apiurl/{listing_ids[i]}/pr', cookies=cookies, headers=headers)
response = response.json()
print(response['jsonValue'])
break
I have an array currently as listing_ids
and within this array has an undetermined count of values that I want to use within a request URL then price the response then loop back through until the the last value within the array was used.我目前有一个数组作为
listing_ids
,并且在这个数组中有一个我想在请求URL中使用的值的未确定计数,然后为响应定价,然后循环返回,直到使用数组中的最后一个值。 How can I format this?我该如何格式化? Currently it just infinitely loops with the 1st value ONLY
目前它只是无限循环只有第一个值
The break
statement in your code breaks the for
loop after it's first iteration, then because the while
loop is still running the for
loop starts all over again.代码中的
break
语句在第一次迭代后中断for
循环,然后因为while
循环仍在运行for
循环重新开始。 Step-by-step what's happening is:逐步发生的事情是:
while
loop starts its first iteration while
循环开始它的第一次迭代for
loop starts its first iteration for
循环开始它的第一次迭代break
is executed - start from step 2 againbreak
- 再次从第 2 步开始If I understood your intent correctly you can just remove the while
loop and the break
statement.如果我正确理解了您的意图,您可以删除
while
循环和break
语句。 Your code will then iterate over the list and make a request for each id.然后,您的代码将遍历列表并对每个 ID 发出请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.