In python, The enumerate function traverses the element usage analysis, and pythonenumerate
This document describes how to use the enumerate function to traverse elements in python. We will share this with you for your reference. The details are as follows:
The enumerate function is used to traverse the elements in the sequence and Their subscript.
The sample code is as follows:
i = 0seq = ['one', 'two', 'three']for element in seq: print i, seq[i] i += 1#0 one#1 two#2 threeprint '============'seq = ['one', 'two', 'three']for i, element in enumerate(seq): print i, seq[i]print '============'for i,j in enumerate('abc'): print i,j#0 a#1 b#2 cprint '============'def _treatment(pos, element): return '%d: %s' %(pos, element)seq = ['one', 'two', 'three']print [_treatment(i, e1) for i, e1 in enumerate(seq)]
The running result is as follows:
0 one1 two2 three============0 one1 two2 three============0 a1 b2 c============['0: one', '1: two', '2: three']
I hope this article will help you with Python programming.
Articles you may be interested in:
- Example Analysis of enumerate function usage in python
- Python advanced tutorial-loop functions range, enumerate, zip
- Instance parsing of enumerate usage in python
- Using the enumerate function in python to traverse element instances
- Python enumerate traversal array example application
- Python recursively traverses all elements in a set
- Summary of python methods for Traversing Arrays
- Python Traversal method of all members in the class