Python编程:for 循环遍历字典.docx
for循环遍历字典在使用for循环遍历字典时,经常会用到和字典相关的3个方法,即items。、keys。以 及values(),它们各自的用法已经在前面章节中讲过,这里不再赘述。当然,如果使用for 循环直接遍历字典,则迭代变量会被先后赋值为每个键值对中的键。例如:1. my die = 'python 教程':. biancheng. net/python/, 2. J shell 教程':"http:c. biancheng. net/shell/八3. ' java 教程:,z. biancheng. net/java/z,)4. for ele in my die:5. print (' ele =,, ele)程序执行结果为:ele = python 教程ele = shell 教程ele = java 教程因此,直接遍历字典,和遍历字典keys()方法的返回值是相同的。除此之外,我们还可以遍历字典values。、items。方法的返回值。例如:1. my die = 'python 教程':. biancheng. net/python/, 2. ' shell 教程':z,. biancheng. net/shell/z,, 3. ' java 教程'/httpV/c. biancheng. net/java/z,4. for ele in my die. items ():5. print (' ele =,, ele)程序执行结果为:ele = ('python 教程丁http:ele = ('shell 教程丁ele = (java 教程http: