如何在Python中中断多个循环?

如何在Python中中断多个循环?,python,Python,我通过一个JSON dict循环来捕获一些数据,我收到了预期的输出,但是太多次了,应该在哪里中断循环来正确捕获输出?此输出显示格式化字符串的次数是否比预期的要多 for dSr in lServiceRequests: dListOfLa311ElectronicWaste = dSr["ListOfLa311ElectronicWaste"] if("La311ElectronicWaste" in dListOfLa311ElectronicW

我通过一个JSON dict循环来捕获一些数据,我收到了预期的输出,但是太多次了,应该在哪里中断循环来正确捕获输出?此输出显示格式化字符串的次数是否比预期的要多

for dSr in lServiceRequests:
            dListOfLa311ElectronicWaste = dSr["ListOfLa311ElectronicWaste"]
            if("La311ElectronicWaste" in dListOfLa311ElectronicWaste):
                lLa311ElectronicWaste = dListOfLa311ElectronicWaste["La311ElectronicWaste"]
                for dla311ElectronicWaste in lLa311ElectronicWaste:
                     v_ewaste_item_count= dla311ElectronicWaste[K_ewaste_Item_Count]
            dListOfLa311ElectronicWaste = dSr["ListOfLa311ElectronicWaste"]
            if("La311ElectronicWaste" in dListOfLa311ElectronicWaste):
                lLa311ElectronicWaste = dListOfLa311ElectronicWaste["La311ElectronicWaste"]
                for dla311ElectronicWaste in lLa311ElectronicWaste:
                    v_ewaste_type = dla311ElectronicWaste[K_ewaste_type]
                v_ewaste_item_info += '{0},  {1} '.format(v_ewaste_item_count, v_ewaste_type)
                print v_ewaste_item_info
输出:

1,  Cell Phone 
 1,  Cell Phone 1,  Computers 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 4,  Laptops/Tablets 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 4,  Laptops/Tablets 4,  Stereo/Speakers 
 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 4,  Laptops/Tablets 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 4,  Laptops/Tablets 4,  Stereo/Speakers 
 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 4,  Laptops/Tablets 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 4,  Laptops/Tablets 4,  Stereo/Speakers 
 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 4,  Laptops/Tablets 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 4,  Laptops/Tablets 4,  Stereo/Speakers 
 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 4,  Laptops/Tablets 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 4,  Laptops/Tablets 4,  Stereo/Speakers 

当您需要突破多个循环时,您可以选择

1-将此代码放在单独的函数中并使用返回

2-使用标志变量了解从第一个循环中断后何时退出外部循环

3-使用异常退出


我相信还有更多的方法可以做到这一点

当您需要突破多个循环时,您可以选择

1-将此代码放在单独的函数中并使用返回

2-使用标志变量了解从第一个循环中断后何时退出外部循环

3-使用异常退出


我相信还有更多的方法可以做到这一点

将以下列表作为示例/类比:

l1 = [x for x in range(10)]
l2 = [x for x in range(20)]
而不是像这样迭代:

for i in l1:
    for j in l2:
        print(i)
        print(j)
for i in l1, l2:
    print(i)
像这样迭代它们:

for i in l1:
    for j in l2:
        print(i)
        print(j)
for i in l1, l2:
    print(i)

尝试将其应用于代码(使用逗号和一个循环,并相应地访问元素)。

将以下列表作为示例/类比:

l1 = [x for x in range(10)]
l2 = [x for x in range(20)]
而不是像这样迭代:

for i in l1:
    for j in l2:
        print(i)
        print(j)
for i in l1, l2:
    print(i)
像这样迭代它们:

for i in l1:
    for j in l2:
        print(i)
        print(j)
for i in l1, l2:
    print(i)

尝试将它应用到代码中(使用逗号和一个循环,并相应地访问元素)。

您可以尝试创建一个异常并捕获它以退出

class BreakAllLoops(Exception):
    print("Breaking loops")

try:
    while True:
        for i in range(10):
            print(i)
            if(i > 5):
                raise BreakAllLoops

except BreakAllLoops:
    print("Left loops")

您可以尝试创建一个异常并捕获它以退出

class BreakAllLoops(Exception):
    print("Breaking loops")

try:
    while True:
        for i in range(10):
            print(i)
            if(i > 5):
                raise BreakAllLoops

except BreakAllLoops:
    print("Left loops")

你不需要打破任何东西。而不是
print v_ewaste_item_info
do

L=[]
for dSr in lServiceRequests:
...
if v_ewaste_item_info not in L:
    L.append(v_ewaste_item_info)

然后打印L。它将不包含重复项。

您不需要破坏任何内容。而不是
print v_ewaste_item_info
do

L=[]
for dSr in lServiceRequests:
...
if v_ewaste_item_info not in L:
    L.append(v_ewaste_item_info)

然后打印L。它将不包含重复项。

至少你需要统一你自己的风格来命名变量…你能向我们解释你的目标和JSON输入是什么吗?至少你需要统一你自己的风格来命名变量…你能向我们解释你的目标和JSON输入是什么吗?