热线电话:13121318867

登录
2018-11-23 阅读量: 928
迭代字典

要遍历字典的所有关键字,可像遍历序列那样使用普通的for语句。

d = {'x': 1, 'y': 2, 'z': 3}

for key in d:

print(key, 'corresponds to', d[key])

也可使用keys等字典方法来获取所有的键。如果只对值感兴趣,可使用d.values。你可能还 记得,d.items以元组的方式返回键值对。for循环的优点之一是,可在其中使用序列解包。

for key, value in d.items():

print(key, 'corresponds to', value)

0.0000
2
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子