for v in d.values():
for k,v in d.items():
for a in 'adfa':
#判断对象是否可迭代
from collections import Iterable
isinstance('abc',Iterable)
#添加下标
for x,y in enumerate([(1,1),(2,3),(4,5)]):
详解1.迭代list
>>> d={'a':1,'b':2,'c':3}
#输出values
>>> for v in d.values():
... print v
...
1
3
2
#同时输出key,value
>>> for k, v in d.items(): ... print k,v ... #输出 a 1 c 3 b 2
2.字符串迭代
>>> for a in 'adfa': ... print a ... a d f a
3.判断一个对象是否可迭代
>>>from collections import Iterable #判断字符串是否可迭代 >>>isinstance('abc',Iterable) #输出 True
#判断list是否可迭代
>>>isinstance([1,2,3],Iterable) #输出 True
#判断整数是否可迭代
>>> isinstance(12345,Iterable)
False
4.对list实现下标循环
>>> for i,value in enumerate(['a','b','c']): ... print(i,value) ... (0, 'a') (1, 'b') (2, 'c') >>> for i,value in enumerate(['a','b','c']): ... print i,value ... 0 a 1 b 2 c
>>> for x,y in [(1,1),(2,3),(4,5)]: ... print x,y ... 1 1 2 3 4 5
>>> for x,y in enumerate([(1,1),(2,3),(4,5)]):
... print x,y
...
0 (1, 1)
1 (2, 3)
2 (4, 5)