简体   繁体   中英

How can I use enumerate function in Python?

for num, data in enumerate(test_data[:len(test_data)]):

I have a code snippet like this. Let's say I have 30 data, in the test data. Is this for loop goes from 0 to 30 or from 0 to 29?

Looks like 0 to 29:

>>> test_data = [n for n in range(0,30)]
>>> for num, data in enumerate(test_data[:len(test_data)]):
...   print(num)
... 
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

You can test it by printing the count.

for i in range(0,4):
    print(i)

Output:

0 1 2 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM