The use of enumerate functions in Python

Source: Internet
Author: User

Describe

The enumerate () function is used to combine a data object that can be traversed (such as a list, tuple, or string) into an index sequence, listing both the data and the data subscript.

That is, for an iterative (iterable)/Ergodic object (such as a list, string), enumerate makes it an index sequence that can be used to obtain both the index and the value.

Typically used in a for loop.

Python 2.3. The above version is available, 2.6 add the start parameter.

Grammar

The following is the syntax for the Enumerate () method:

Enumerate (sequence[,startindex=0])
Parameters
    • Sequence--a sequence, iterator, or other supporting iteration object.
    • Start-the subscript start position.
return value

Returns the enumerate (enum) object.

Believe that everyone must see indefinitely, Foggy Bar, see the example ~

Instance
season=['Spring','Summer','Fall','Winter']Print(Enumerate (season))Print(List (enumerate (season))) forIinchEnumerate (season):Print(i) forI,elementinchEnumerate (season):Print(I,season[i])Print(i)

The output is:

<enumerate Object at 0x000002ce4c2ec870>
 [(0,  " spring  ", (1,  "  Summer   "), (2,  '  fall  " ), (3,  " winter   ")] 
' Spring ' ('summer') ('fall' ) ( 'winter')
0 spring01 summer fall Winter3

4 print outputs, with 4 result blocks specially made:

First look at the first Print,print (enumerate (season)), and the return result is

<enumerate Object at 0x000002ce4c2ec870>

A memory sequence value is returned, indicating that the sequence in memory points to ' 0x000002ce4c2ec870 '. But it doesn't seem like much, is it, the following example will tell us that enumerate is indeed a sequence of returns.

The second print,print (list (enumerate (season))) returns the result

[(0, ' Spring '), (1, ' Summer '), (2, ' Fall '), (3, ' Winter ')]

The return is a set of sequences, and we can be quite sure that this is a list object, because with the list keyword. But it's not like, indeed, we do not accept the flicker, only accept the naked reality, please look down.

Third print,for I in Enumerate (season): Print (i), the result returned is

(0, ' Spring ') (1, ' Summer ') (2, ' fall ') (3, ' Winter ')

This returns the combination of the subscript and the corresponding value, with the default subscript starting at 0 and, if startindex is specified, starting with the specified number. At this point, we should be able to determine that the enumerate return value is indeed a sequence.

Fourth Print,for i,element in Enumerate (season): Print (I,season[i]) print (i), the result returned is

0 spring01 summer12 fall23 winter3

This time is a double variable, from the returned results, the first variable is the index subscript, the second is the value subscript corresponding values.

This looks like enumerate () function also nothing, don't mess, next May refresh three view Oh ~

The above example enumerate () object argument is a list, but enumerate can be more than that, the document is interpreted to be able to traverse the object is available enumerate, which also includes the string! Please see:

abc='abcdefg'print(enumerate (ABC))print(list ( Enumerate (ABC ))) for in Enumerate (ABC):    print(i)   for in enumerate (ABC):    print(I,abc[i])

The output result is

<enumerate Object at 0x000001b75a712828>[(0,'a'), (1,'b'), (2,'C'), (3,'D'), (4,'e'), (5,'F'), (6,'g')] (0,'a')(1,'b')(2,'C')(3,'D')(4,'e')(5,'F')(6,'g') 0 A1b2C3D4e5F6 g

Exactly the same as the list, not surprising?!

Enumerate there are other uses, we first create a text document Abd123.txt, with the help of the 12 zodiac, each line to write an animal, such as "I am a mouse",

It is now required to output the number of rows.

First, use the Len () function directly.

F=open ('c:\\users\ronghere\desktop\\abc123.txt','r') G=f.readlines ()print(len (g))

The output is 12, correct. But if the value is very large, then the efficiency is not significant ~

Second, use the enumerate () function.

F=open ('c:\\users\ronghere\desktop\\abc123.txt','r') G=f.readlines ()print(len (g)) Count=0 for in Enumerate (g):    + = 1print(count)

The output is also 12, but the efficiency is much higher than before.

PS, try not to nest multi-layered functions, which will consume resources, and even error, more use of temporary storage variables, this is a good coding habits. For example, this time, originally did not want to introduce the variable G, but nested into the found error, so temporary storage is still necessary, and the variables can be del off, to avoid memory consumption.

The use of enumerate functions in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.