我已经创建了一个迭代多级字典的函数,并执行需要四个参数的第二个函数ssocr
:coord,background,foreground,type(它们是我的键的值) 。这是我的字典,取自json文件。
def parse_image(self, d):
bg = d['background']
fg = d['foreground']
results = {}
for k, v in d['boxes'].iteritems():
if 'foreground' in d['boxes']:
myfg = d['boxes']['foreground']
else:
myfg = fg
if k != 'players_home' and k != 'players_opponent':
results[k] = MyAgonism.ssocr(v['coord'], bg, myfg, v['type'])
results['players_home'] = {}
for k, v in d['boxes']['players_home'].iteritems():
if 'foreground' in d['boxes']['players_home']:
myfg = d['boxes']['players_home']['foreground']
else:
myfg = fg
if k != 'background' and k != 'foreground':
for k2, v2 in d['boxes']['players_home'][k].iteritems():
if k2 != 'fouls':
results['players_home'][k] = {}
results['players_home'][k][k2] = MyAgonism.ssocr(v2['coord'], bg, myfg, v2['type'])
return results
在最后一个iteritems中,我只为name
键获得了正确的值。 score
键不会出现。在name
词典
score
覆盖results['players_home']
似乎如此
输出:... "player4": {"name": 9}, "player5": {"name": 24} ...
我想要... "player4": {"name": 9, "score": value}, "player5": {"name": 24, "score": value} ...
我做错了什么?以下是完整代码:Full Code
答案 0 :(得分:0)
这可能是一个问题:
if k2 != 'fouls': results['players_home'][k] = {}
在您的循环中,每次k2
不是'fouls'
时,您都会创建一个新的空字典并将其存储在results['players_home']
中。这意味着先前存储在那里的任何条目都不再可访问。