Python打印字典键和值并排 [英] Python Printing Dictionary Key and Value side by side

查看:875
本文介绍了Python打印字典键和值并排的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个程序,用于并排打印以下代码的键和值:

I want a program that prints Key and Value side by side for the following code:

这是字典:

d = {'M': ['Name1', 'Name2', 'Name3'], 'F': ['Name1','Name2','Name3']}

我想要一个以以下形式打印的程序:

I want the a program that prints in the following form:

M, Name1
M, Name2
M, Name3
F, Name1
F, Name2
F, Name3     

推荐答案

d = {'M': ['Name1', 'Name2', 'Name3'], 'F': ['Name1','Name2','Name3']} 

for key in d.keys():
    for value in d[key]:
        print key,value

一个更优雅的解决方案可能是:

A more elegant solution may be:

for key,value in d.iteritems():
    print key,value

这篇关于Python打印字典键和值并排的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆