In python, The enumerate function traverses the element usage analysis, and pythonenumerate

Source: Internet
Author: User
Tags array example python enumerate

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

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.