Python enumerate遍历数组示例应用
2019/7/13 22:03:46
本文主要是介绍Python enumerate遍历数组示例应用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
其他语言中,比如C#,我们通常遍历数组是的方法是:
for (int i = 0; i < list.Length; i++)
![](/upload/201907/13/201907132203460693.gif)
![](/upload/201907/13/201907132203461162.gif)
{
//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)
![](/upload/201907/13/201907132203460215.gif)
![](/upload/201907/13/201907132203460693.gif)
![](/upload/201907/13/201907132203461162.gif)
![](/upload/201907/13/201907132203461503.gif)
![](/upload/201907/13/201907132203461876.gif)
![](/upload/201907/13/201907132203462304.gif)
在Python中,我们习惯这样遍历:
![](/upload/201907/13/201907132203462644.gif)
![](/upload/201907/13/201907132203463002.gif)
这样遍历取不到item的序号i,所有就有了下面的遍历方法:
![](/upload/201907/13/201907132203463301.gif)
![](/upload/201907/13/201907132203463721.gif)
其实,如果你了解内置的enumerate函数,还可以这样写:
![](/upload/201907/13/201907132203464150.gif)
![](/upload/201907/13/201907132203464551.gif)
这篇关于Python enumerate遍历数组示例应用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python