Enumerate usage in Python

Source: Internet
Author: User
Tags python enumerate

Enumerate () is a python built-in function for python2.x and python3.x
Enumerate are enumerated and enumerated in the dictionary.
The enumerate parameter is an object that can be traversed/iterated (such as a list, a string)
The enumerate is used to get a count in the for loop, which can be used to get both the index and the value, which is required when the index and value values are needed enumerate
Enumerate () Returns a enumerate object

>>> LST = [1, 2, 3, 4, ten, 5]>>> Enumerate (LST)<enumerate object at 0x00000000032a3 990>

Use of Enumerate:
For example: LST = [1,2,3,4,5,6] is known to require output:
0,1
The
2,3
3,4
4,5
5,6

>>> lst = [1,2,3,4,5,6] for in Enumerate (LST):  print ('  %s,%s' % (index,value))  0,11,22,33,44,55,6
#The specified index starts at 1>>> LST = [1,2,3,4,5,6]>>> forIndex,valueinchEnumerate (lst,1):Print('%s,%s'%(index,value))1,12,23,34,45,56,6#The specified index starts at 3>>> forIndex,valueinchEnumerate (lst,3):Print('%s,%s'%(index,value))3,14,25,36,47,58,6

Add:
If you want to count the number of lines in a file, you can write:
Count = Len (open (filepath, ' R '). ReadLines ())
This method is simple, but may be slow, and cannot even work when the file is larger.

You can use enumerate ():
Count = 0
For index, line in enumerate (open (filepath, ' R ')):
Count + = 1

Resources:
Python Enumerate usage Summary: http://blog.csdn.net/churximi/article/details/51648388

Enumerate usage in Python

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.