When processing various types of sequences in python, if we want to display the elements of this sequence and their subscripts , we can use enumerate()function.
enumerate() function is used to traverse the elements in the sequence and their subscripts. The usage is as follows:
1. The parameter is a tuple:
for index, value in enumerate(('a', 'b', 'c')): '''下标,元素''' print(index,value)
2..The parameter is a listlist:
for index, value in enumerate([1, 2, 3]): '''下标,元素''' print(index, value)
3.The parameter is a stringstring:
for index, value in enumerate('abc'): '''下标,元素''' print(index, value)
4. The parameter is a dictionary dict:
for index, value in enumerate({'a': 1, 'b': 2}): '''下标(字典无序性),元素(Key值)''' print(index, value)
The above is the detailed content of How to use the enumerate() function in python. For more information, please follow other related articles on the PHP Chinese website!