-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
它允许我们遍历数据并自动计数
for counter, value in enumerate(some_list):
print(counter, value)choices = ["first",'second','third','fourth','fifth']
#start为可选参数 默认从 0 开始
for index, item in enumerate(choices, start = 1):
print index, item
#(1,"first")
#(2,"second")
#(3,"third")
#(4,"fourth")
#(5,"fifth")
#也可以这样表示
for index, item in enumerate(choices):
print index + 1, itemmy_list = ['apple', 'banana', 'grapes', 'pear']
for c, value in enumerate(my_list, 1):
print(c, value)
# 输出:
(1, 'apple')
(2, 'banana')
(3, 'grapes')
(4, 'pear')Metadata
Metadata
Assignees
Labels
No labels