When traversing a sequence in python, we usually take the following approach:
For item in sequence:
process (item)
If you want to take the location of an item, you can write this:
For index in range (len (sequence)):
process (Sequence[index])
Another better way is to use the enumerate function built in Python:
Enumerate (sequence,start=0)
In the above function, sequence is an iterative object, which can be a list, a dictionary, a file object, and so on. Enumerate returns a tuple of subscript and item:
>>> seasons = [' Spring ', ' Summer ', ' Fall ', ' Winter ']
>>> list (Enumerate (seasons)) [(0, ' Spring '), (1, ' Summer '), (2, ' Fall '), (3, ' Winter ')]
>>> list (Enumerate (seasons, Start=1)) [(1, ' Spring ')], (2, ' Summe R '), (3, ' Fall '), (4, ' Winter ')]
The first example of the article can then be written in this way:
For Index,item in Enumerate (sequence):
print Index,item
Above this python gets the list subscript and its value the simple method is the small series to share to everybody's content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.