概念:可用于循环就是迭代
#!/usr/bin/python
dic = {'a':1, 'b':2, 'c':3, 'd':4};
for key in dic:
print "key:", key;
for value in dic.itervalues():
print "value:", value;
for key in dic.iterkeys():
print "key:", key;
for key in dic.keys():
print "keys:", key;
for key,value in dic.iteritems():
print "iteritems:", key, value;
#check 检查dic是否为可迭代类型
from collections import Iterable #申明Iterable
print isinstance(dic, Iterable);
#enumerate
for x, y in enumerate(['a', 'b', 'c']):
print x, y
for x,y in [(1, 2), (3, 4), (5, 6)]:
print x,y
for x in [(1, 2), (3, 4), (5, 6)]:
print x
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。