简体   繁体   English

Python不会通过嵌套循环进行迭代

[英]Python doesn't iterate through nested loop

I have a nested loop inside a function that does not seem to be working. 我在似乎无法正常工作的函数中有一个嵌套循环。

Both the keys and values of the 'broadcast' dictionary exist (are non-empty) and print to stdout. 'broadcast'字典的键和值都存在(非空)并打印到stdout。

When I try to print out in a loop (the second loop) what I think should be broadcast's values, it is empty - this does not make sense to me because I clearly see them printed out from the first loop when I use 当我尝试在一个循环(第二个循环)中打印出我认为应该广播的值时,该值为空-这对我来说没有意义,因为当我使用时,我清楚地看到它们是从第一个循环中打印出来的

print(broadcast.values())  

The first chunk below is my code and following it is what is printed out to stdout: ( ... displayed where output continues) 下面的第一个块是我的代码,其后是将其输出到stdout的内容:( ...显示继续输出的位置)

def broadcast_display(results):
    broadcast = results['broadcast']
    thumbnails = results['thumbnails']
    html = []
    html.append('\n<h2 style="text-align:center;">Broadcast / Receive</h2>')
    message = ""
    print('broadcasts keys')
    print(broadcast.keys())
    print('broadcasts values')
    print(broadcast.values())
    for type, lists in broadcast.items():
        print('looping through broadcasts values')
        print(lists)                                                                               
        for list in lists:
            #Does not reach this loop                                                               
            print('TEST 3: does not print')
            #code goes on
            html.append('\n<hr>')
            html.append('\n<h2>{0}</h2>'.format(KelpPlugin.SCRIPT_TITLES[blocktype])) #heading                          
            html.append('\n<table border = "1">')
            for sprite, script in blocklist:
                if KelpPlugin.script_start_type(script) == KelpPlugin.HAT_WHEN_I_RECEIVE:
                    # check if the message is the same as the last one                                                  
                    # if it is, print this script next to the last                                                      
                    # otherwise, print it below the last                                                                
                    if message != script[0].args[0].lower():
                        html.append('\n  </tr>')
                        html.append('\n  <tr>')
                    message = script[0].args[0].lower()
                    script_images = KelpPlugin.to_scratch_blocks(sprite, script)
                    html.append('\n<td>')
                    html.append('\n<p>        {0}</p>'.format(sprite))
                    html.append('\n    <p><img src="{0}" height="100" width="100"></p>'.format(thumbnails[sprite]))
                    html.append('\n<pre class="blocks">')
                    html.append('\n<p>{0}</p>'.format(script_images))
                    html.append('\n</pre>')
                    html.append('\n</td>')
                elif KelpPlugin.script_start_type != KelpPlugin.NO_HAT:
                    if message == "":
                        html.append('\n  </tr>')
                    html.append('\n  <tr>')
                    script_images = KelpPlugin.to_scratch_blocks(sprite, script)
                    html.append('\n<p>{0}</p>'.format(sprite))
                    html.append('\n    <p><img src="{0}" height="100" width="100></p>'.format(thumbnails[sprite]))
                    html.append('\n<pre class="blocks">')
                    html.append('\n<p>{0}</p>'.format(script_images))
                    html.append('\n</pre>')
                    html.append('\n  </tr>')
            html.append('\n</table>')
            return ''.join(html)

broadcasts keys
[0, 2, 3]
broadcasts values
[[], [[(u'LosAngeles', kurt.Script([
    kurt.Block('whenClicked'), ...
looping through broadcasts values
[]  

The results dictionary looks like this: 结果字典如下所示:

{0: [], 2: [[(u'San Juan Batista', kurt.Script([
kurt.Block('whenClicked'),
kurt.Block('broadcast:', u'SJB')], pos=(69, 59.0))), (u'Missionary', kurt.Script([
kurt.Block('whenIReceive', u'SJB'), ... 3: []}

Also this is my first time asking a question on StackOverflow so go easy on me please :) 这也是我第一次问关于StackOverflow的问题,所以请对我轻松一点:)

I just tried with a sample code and I am able to reach the inner loop. 我只是尝试了一个示例代码,并且能够到达内部循环。 Here: 这里:

di = {'0': ['a1', 'a2', 'a3'], '2': ['b1', 'b2', 'b3'], '3': 'c'}
for k,v in di.items():
    print k
    for item in v:
        print "\t" + item

Output: 输出:

$ python t.py
0
    a1
    a2
    a3
3
    c
2
    b1
    b2
    b3
$

The values that you specify seem to not have a properly formed loop. 您指定的值似乎没有正确形成的循环。 Can you check that. 你能检查一下吗? As per the key/values that you have provided, here is how your dictionary should look like? 根据您提供的键/值,这是字典的外观? Can you please specify that properly. 您能正确指定吗?

0: [[], 
2: [[(u'LosAngeles', kurt.Script([
    kurt.Block('whenClicked'), 
3: ... looping through broadcasts values []  

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

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