简体   繁体   English

python中的随机迭代次数后,循环返回false

[英]Loop returns false after random number of iteration in python

In the below code 在下面的代码中

import pyzipcode as pyzip
location = []
for var in grouped_list_long_zip:
    holder = pyzip.Pyzipcode.get(var[0][4], 'US', return_json=False)
    location.append(holder['location'])

grouped_list_long_zip is a list of list of list which contains locations where the fourth index in each sublist is the zipcode. grouped_list_long_zip是列表列表的列表,其中包含每个子列表中第四个索引是邮政编码的位置。 Using this and the module pyzipcode , I want to return latitude and longitude location of the zipcodes and store them in location . 使用此代码和pyzipcode模块,我想返回pyzipcode纬度和经度位置,并将其存储在location My problem is after a random number of iteration the function will return false and make holder a bool type. 我的问题是,在随机次数的迭代之后,该函数将返回false并使holderbool类型。 The most common iteration it fails on is the 11th, but it also happens on other iterations. 它失败的最常见迭代是第11次,但其他迭代也发生了。 I am unsure of how to debug this problem. 我不确定如何调试此问题。

Edit: 编辑:

When the code fails var simply equals what grouped_lsit_long_zip equals at that location. 当代码失败时, var等于该位置的grouped_lsit_long_zip等于。

I would modify the code like that to get some feedback on the values that disrupt the execution . 我将像这样修改代码,以获取有关破坏执行的值的一些反馈。

import pyzipcode as pyzip
location = []

for var in grouped_list_long_zip:
    holder = pyzip.Pyzipcode.get(var[0][4], 'US', return_json=False)
    if type(holder) == bool:
        print(var[0])
    else:
        location.append(holder['location'])

Another thing you can do is wrapping the thing in a try-except block to simply ignore the problematic cases if an error is thrown. 您可以做的另一件事是将其包装在try-except块中,以仅在抛出错误时忽略有问题的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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