首页 > 代码库 > dict两种遍历方法

dict两种遍历方法

采用for...in...遍历:

>>> for i in dd:...     print("%s:%s"%(i,dd[i]))...1:chen2:hang3:wust4:wang5:yadan6:wuhan8:xinzhou9:haha

采用dd.items()遍历:

>>> for k,v in dd.items():...     print("%s:%s"%(k,v))...1:chen2:hang3:wust4:wang5:yadan6:wuhan8:xinzhou9:haha

 

dd为:

>>> dd{1: chen, 2: hang, 3: wust, 4: wang, 5: yadan, 6: wuhan, 8: xinzhou, 9: haha}

 

dd

dict两种遍历方法