or 循环遍历字典
在使用 for 循环遍历字典时,经常会用到和字典相关的 3 个方法,即 items()、keys() 以及 values(),它们各自的用法已经在前面章节中讲过,这里不再赘述。当然,如果使用 for 循环直接遍历字典,则迭代变量会被先后赋值为每个键值对中的键。例如:
my_dic = {'python教程':"https://www.niwoxuexi.com/python/", 'shell教程':"https://www.niwoxuexi.com/shell/", 'java教程':"https://www.niwoxuexi.com//java/"} for ele in my_dic: print('ele =', ele)
程序执行结果为:
ele = ('python教程', 'https://www.niwoxuexi.com/python/')
ele = ('shell教程', 'https://www.niwoxuexi.com/shell/')
ele = ('java教程', 'https://www.niwoxuexi.com/java/')
标签:
for 循环遍历字典