1. 程式人生 > >python之迭代

python之迭代

Iterable是迭代在各種語言中常見,在API中的解釋

在python中主要通過for  in 實現

迭代物件用isinstance(str,Iterable)去判斷則要植入collections集合庫

最後一個小問題,如果要對list實現類似Java那樣的下標迴圈怎麼辦?Python內建的enumerate函式可以把一個list變成索引-元素對,這樣就可以在for迴圈中同時迭代索引和元素本身:

for i,value in enumerate("abcd"):

  print(i,value)