简体   繁体   English

如何在 Python 中使用枚举 function?

[英]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.假设我在测试数据中有 30 个数据。 Is this for loop goes from 0 to 30 or from 0 to 29?这个 for 循环是从 0 到 30 还是从 0 到 29?

Looks like 0 to 29:看起来像 0 到 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: Output:

0 1 2 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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