简体   繁体   English

如何迭代和循环遍历python中的每个数组值

[英]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:逐步发生的事情是:

  1. The while loop starts its first iteration while循环开始它的第一次迭代
  2. The for loop starts its first iteration for循环开始它的第一次迭代
  3. First request is made for the first id第一个请求是针对第一个 id
  4. First JSON value is printed打印第一个 JSON 值
  5. break is executed - start from step 2 again执行break - 再次从第 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM