[英]Unable to process dict properly
我有得到列表的 dict,我想要的是在一行中显示列表元素。
我的听写
data = {'id':[1,2], 'name':['foo','bar'], 'type':['x','y']}
预期 output
name is foo and id is 1
name is bar and id is 2
我的代码
>>> data = {'id':[1,2], 'name':['foo','bar'], 'type':['x','y']}
>>> for key, value in data.items():
... print(value)
...
['foo', 'bar']
['x', 'y']
[1, 2]
>>>
您可以使用zip()
作为:
for name, idx in zip(data['name'], data['id']):
print(f"name is {name} and id is {idx}")
如果您使用的是低于 3.6 的 python 版本,请使用format()
:
for name, idx in zip(data['name'], data['id']):
print("name is {0} and id is {1}".format(name, idx))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.