Aur*_*ger 2 python dictionary list python-3.x
我有一个字典值列表,如下所示:
{'ID 2': [{'game': 586, 'start': 1436882375, 'process': 13140}, {'game': 585, 'start': 1436882375, 'process': 13116}], 'ID 1': [{'game': 582, 'start': 1436882375, 'process': 13094}, {'game': 583, 'start': 1436882375, 'process': 12934}, {'game': 584, 'start': 1436882375, 'process': 12805}]}
Run Code Online (Sandbox Code Playgroud)
它几乎不可读.我想这样格式化:
'ID 2' : {'game': 586, 'start': 1436882375, 'process': 13140},
'ID 2' : {'game': 585, 'start': 1436882375, 'process': 13116},
'ID 1' : {'game': 582, 'start': 1436882375, 'process': 13094},
'ID 1' : {'game': 582, 'start': 1436882375, 'process': 13094},
Run Code Online (Sandbox Code Playgroud)
我的打印代码如下:
for key in queue_dict.items():
for values in key.values():
print(key+" : "+values)
Run Code Online (Sandbox Code Playgroud)
错误是:
AttributeError: 'tuple' object has no attribute 'values'
Run Code Online (Sandbox Code Playgroud)
我无法理解如何访问每个键的列表值中的每个字典.我经常搜索,但找不到答案.有人可以帮忙吗?
dict.items 返回键值对的元组.
返回字典项目的新视图((键,值)对)
你必须这样做.
for key,values in queue_dict.items():
for v in values:
print(key," : ",v)
Run Code Online (Sandbox Code Playgroud)
此外,您无法使用连接str对象和dict对象+.你会得到一个TypeError.所以你必须把它投到一个str.相反,您可以使用格式print("{} : {}".format(key,v))