首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Python中中断多个循环?

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

Stack Overflow用户
提问于 2015-06-16 02:02:03
回答 4查看 96关注 0票数 0

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

代码语言:javascript
复制
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

输出:

代码语言:javascript
复制
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 
EN

回答 4

Stack Overflow用户

发布于 2015-06-16 02:05:17

当您需要跳出多个循环时,您可以选择。

1-将这段代码放在一个单独的函数中,并使用一个return。

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

3-使用异常退出

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

票数 4
EN

Stack Overflow用户

发布于 2015-06-16 02:10:55

您可以随时尝试并创建一个异常,然后捕获它以使其退出。

代码语言:javascript
复制
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")
票数 1
EN

Stack Overflow用户

发布于 2015-06-16 02:19:31

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

代码语言:javascript
复制
L=[]
for dSr in lServiceRequests:
...
if v_ewaste_item_info not in L:
    L.append(v_ewaste_item_info)

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30851942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档