python|python enumerate函数用法
- enumerate is a built-in function of Python. It allows us to loop over something and have an automatic counter.
for counter, value in enumerate(some_list):
print(counter, value)
- enumerate also accepts an optional argument, The optional argument allows us to tell enumerate from where to start the index.
my_list = ['apple', 'banana', 'grapes', 'pear']
for i, val in enumerate(my_list, 1):
print (i, val)# Output:
# 1 apple
# 2 banana
# 3 grapes
# 4 pear
- enumerate can also be used create tuples containing the index and list item using a list.
my_list = ['apple', 'banana', 'grapes', 'pear']
counter_list = list(enumerate(my_list, 1))
print (counter_list)
# Output: [(1, 'apple'), (2, 'banana'), (3, 'grapes'), (4, 'pear')]
【python|python enumerate函数用法】Reference:
http://book.pythontips.com/en/latest/enumerate.html
推荐阅读
- python学习之|python学习之 实现QQ自动发送消息
- 逻辑回归的理解与python示例
- 一起来学习C语言的字符串转换函数
- C语言字符函数中的isalnum()和iscntrl()你都知道吗
- C语言浮点函数中的modf和fmod详解
- python自定义封装带颜色的logging模块
- C语言中的时间函数clock()和time()你都了解吗
- 【Leetcode/Python】001-Two|【Leetcode/Python】001-Two Sum
- Python基础|Python基础 - 练习1
- Python爬虫|Python爬虫 --- 1.4 正则表达式(re库)