Python enumerate遍历数组示例应用

WBOY
Release: 2016-06-16 08:47:32
Original
1303 people have browsed it

其他语言中,比如C#,我们通常遍历数组是的方法是:

for (int i = 0; i list.Length; i++)
{
    
//todo with list[i]
}

在Python中,我们习惯这样遍历:
for item in sequence:
   
process(item)

这样遍历取不到item的序号i,所有就有了下面的遍历方法:
for index in range(len(sequence)):
    process(sequence[index])

其实,如果你了解内置的enumerate函数,还可以这样写:
for index, item in enumerate(sequence):
    process(index, item)
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!