Python progresses a little bit every day---> enumerate

Source: Internet
Author: User
Tags define function

Enumerate, enumeration functions

Return an enumerate object,enumerate was useful for obtaining an index list


Example 1: Generate a list and display its index

Import strings = List (string.uppercase) s = s[0:5]for i in range (len (s)): print I,s[i]


Example 2: generate a list and display its index

Import strings = List (string.uppercase) s = s[0:8]for index,i in enumerate (s): Print Index,i


Example 3: Define function Enumerate, pass in a sequence, generate a iterable

Import stringdef Enumerate (sequence, start=0): For i in Sequence:yield start,i start + = 1 List1 = String.uppercasedict1 = {"1": "A", "2": "B", "3": "C",}iterable_1 = Enumerate (list1) iterable_2 = Enumerate (DICT1) p Rint iterable_1.next () print iterable_1.next () print iterable_1.next () print iterable_2.next () print iterable_2.next () Print Iterable_2.next ()


This article from "Tornado" blog, declined reprint!

Python progresses a little bit every day---> enumerate

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.