Python,如何在每一行中打印字典键及其值? [英] Python, how to print dictionary key and its values in each line?
本文介绍了Python,如何在每一行中打印字典键及其值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请参见下图作为参考:
推荐答案
您可以使用一个 for循环
在每次迭代中解压值列表来实现。
You could do it with a single for loop
unpacking a value list each iteration.
d = {1: [2, 3], 2: [4, 5]}
for k in d:
x, y = d[k]
print("{} : {}\n{} : {}".format(k, x, k, y))
1 : 2
1 : 3
2 : 4
2 : 5
因为值列表只有几个数字,所以也可以这样做,例如:
Because the value lists have just a couple numbers, it can also be done, like so:
for k, v in d.items():
print("{} : {}\n{} : {}".format(k, v[0], k, v[1]))
这篇关于Python,如何在每一行中打印字典键及其值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文