python之枚舉python
內置函數 enumearate()函數
list=["這","是","一個","測試"] for i in range(len(list)): print(i,list[i])
list=["這","是","一個","測試"] for index,iterm in enumerate(list): print(index,iterm)
list=["這","是","一個","測試"] for index,iterm in enumerate(list,1): print(index,iterm)
若是要統計文件的行數,能夠這樣寫:測試
count = len(open(filepath, 'r').readlines())
這種方法簡單,可是可能比較慢,當文件比較大時甚至不能工做。spa
能夠利用enumerate():code
count = 0 for index, line in enumerate(open(filepath,'r')): count += 1