请帮助我在以下情况下使用 python 3.7 获取代码: 我有一个元组列表作为输入,例如: 盒子 = [(10,40,50,20),(15,40,50,20)] 我想创建一个字典列表,以便我的输出采用以下格式:
[
{
"top":10,
"right":40,
"bottom":50,
"left":20
},
{
"top":15,
"right":40,
"bottom":50,
"left":20
}
]
我试过 json.dumps() 但没有得到预期的格式
请您参考如下方法:
boxes = [(10,40,50,20),(15,40,50,20)]
dicts = []
for b in boxes:
top, right, bottom, left = b
dicts.append({'top': top, 'right': right, 'bottom': bottom, 'left': left})
print(dicts)