迭代定義
- 通過
for...in
循環(huán)進(jìn)行的遍歷稱為迭代
常見迭代對(duì)象
list
tuple
dict
set
string
判斷迭代對(duì)象
- 引入包
from collections import Iterable
- 執(zhí)行判斷
>>> from collections import Iterable
>>> isinstance('simple',Iterable)
True
>>> isinstance({'key'},Iterable)
True
>>> isinstance({'key':98},Iterable)
True
>>> isinstance((54,77),Iterable)
True
>>> isinstance([54,77],Iterable)
True
>>> isinstance(98,Iterable)
False
利用下標(biāo)迭代
>>> for i,value in enumerate('string'):
... print(i,value)
...
0 s
1 t
2 r
3 i
4 n
5 g
雙變量迭代
>>> for x,y in [(34,65),(45,45),(8,9),(21,3)]:
... print(x,y)
...
34 65
45 45
8 9
21 3