其他語言中,比如C#,我們通常遍歷數(shù)組是的方法是:
for (int i=0; i<list.Length; i++)
{list[i]}
在Python中,我們習(xí)慣這樣遍歷:
for value in list: print(value)
這樣遍歷取不到item的序號(hào)i,所有就有了下面的遍歷方法:
for index in range(len(list)): print(list[index])
現(xiàn)在介紹一個(gè)內(nèi)置的enumerate函數(shù),同時(shí)打印index和value:
for index, value in enumerate(list): print(index, value)
實(shí)例如下:
