Home  >  Article  >  Backend Development  >  What does enumerate mean in python?

What does enumerate mean in python?

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-21 13:51:4115688browse

enumerate(iteration, start): Returns an enumerated object. The iterator (iteration) must be another supported iteration object. The initial value defaults to zero, that is, if you do not enter start, it means starting from zero. The input of the iterator can be a list, string, set, etc., because these are the objects that are iterated. Returns an object. If you express it in the form of a list, it is a list. Each element of the list is a tuple. The ancestor has two elements. The first element represents the number, which means the number of elements. The second element is the corresponding element of the iterator, which is when the default start is zero. If it is not zero, it means that the first element of the first tuple of the list is the value of start, and the subsequent ones are accumulated in sequence, and the second element still has the same meaning.

What does enumerate mean in python?

enumerate(X, [start=0])

The parameter X in the function can be an iterator (iterator) or a sequence. start is the starting count value, starting from 0 by default.

Related recommendations: "Python Video Tutorial"

X can be a dictionary or a sequence.

>>> a = {1: 1, 2: 2, 3: 3}
>>> for i , item in enumerate(a):
       print i, item
Ouput:
1
2
3
>>> b = [1,2,3,4,5,6]
>>> for i , item in enumerate(b):
       print i, item
Ouput:
1
2
3
4
5
6
>>> for i , item in enumerate(b, start=10):
        print i, item
Ouput:
1
2
3
4
5
6

enumerate can also be used to count the number of file lines and can handle larger files.

count = 0
file_count = open(filepath,'r')
for index, line in enumerate(file_count): 
    count += 1
print count

The above is the detailed content of What does enumerate mean in python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn