我在一个似乎没有工作的函数中有一个嵌套循环。
'broadcast'字典的键和值都存在(非空)并打印到stdout。
当我尝试在循环中打印出来时(第二个循环)我认为应该是广播的值,它是空的 - 这对我来说没有意义,因为我清楚地看到它们在我使用时从第一个循环打印出来
print(broadcast.values())
下面的第一个块是我的代码,然后是打印到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
[]
结果字典如下所示:
{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: []}
这也是我第一次在StackOverflow上问一个问题,所以请轻松告诉我:)
答案 0 :(得分:0)
我刚尝试了一个示例代码,我能够进入内循环。这里:
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
输出:
$ python t.py
0
a1
a2
a3
3
c
2
b1
b2
b3
$
您指定的值似乎没有正确形成的循环。你能检查一下吗?根据您提供的键/值,这里是您的字典应该是什么样子?你能否正确说明一下。
0: [[],
2: [[(u'LosAngeles', kurt.Script([
kurt.Block('whenClicked'),
3: ... looping through broadcasts values []