python Iteration(叠代)
阿新 • • 發佈:2018-07-16
key type port vpd src ins pro ges enumerate 概念:可用於循環就是叠代
#!/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
python Iteration(叠代)