我循环通过一个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 发布于 2015-06-16 02:05:17
当您需要跳出多个循环时,您可以选择。
1-将这段代码放在一个单独的函数中,并使用一个return。
2-使用标志变量来知道从第一个中断后何时退出外部循环
3-使用异常退出
我相信还有更多的方法可以做到这一点!
发布于 2015-06-16 02:10:55
您可以随时尝试并创建一个异常,然后捕获它以使其退出。
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")发布于 2015-06-16 02:19:31
你不需要打破任何东西。而不是print v_ewaste_item_info做
L=[]
for dSr in lServiceRequests:
...
if v_ewaste_item_info not in L:
L.append(v_ewaste_item_info)然后打印L。它将不包含重复项。
https://stackoverflow.com/questions/30851942
复制相似问题