Enumerate ()--Python

Source: Internet
Author: User

#!usr/bin/env python#Coding:utf-8" "Enumerate () Description: 1, enumerate () is a python built-in function, 2, enumerate is literally enumerated, enumerated meaning, 3, for an iterative, can traverse the object (list, tuple, string), enumerate to make it a An index sequence, which can be used to obtain both the index and the value, 4, enumerate more for the for loop to get counted, 5, note: Enumerate () returned is the enumerate object;" "" "requirement: Given a string of number 01098040234, the position and value of the number not 0 is printed and output;" "str1='01098040234'defFindnotzero (str):return((Index,value) forIndex,valueinchEnumerate (str)ifvalue!='0')Print(Findnotzero (STR1))

5. Note: Enumerate () Returns the enumerate object-the result of the operation is as follows

<generator object findnotzero.<locals>.<genexpr> at 0x00000223a8416a98> in 0.1s]

#!usr/bin/env python#Coding:utf-8" "Enumerate () Description: 1, enumerate () is a python built-in function, 2, enumerate is literally enumerated, enumerated meaning, 3, for an iterative, can traverse the object (list, tuple, string), enumerate to make it a An index sequence, which can be used to obtain both the index and the value, 4, enumerate more for the for loop to get counted, 5, note: Enumerate () returned is the enumerate object;" "" "requirement: Given a string of number 01098040234, the position and value of the number not 0 is printed and output;" "str1='01098040234'defFindnotzero (str):return((Index,value) forIndex,valueinchEnumerate (str)ifvalue!='0')Print(List (Findnotzero (STR1)))

Operation Result:

' 1 ' ' 9 ' ' 8 ' ' 4 ' ' 2 ' ' 3 ' ' 4 '   in 0.1s]

Note the point:

1, enumerate () specify the starting subscript enumerate (str,1)

2. List parsing type: [Expr for Iter_var in iterable if COND_EXPR]

Advantages of Enumerate ():

Requirements: give you a list of [' I ', ' yes ', ' cold ', ' Yue ']; now you want to simultaneously print out the index position and value of each list element;

The most straightforward approach is to:

 for inch Range (len (list1)):     Print (I,list1[i])

Operation Result:

0 I 1 is 2 cold 3 in 0.2s]

The above method is somewhat cumbersome, and the use of enumerate () will be more direct and graceful:

List1 = [' i ',' yes ',' cold ',' Yue ' ] for in Enumerate (list1):    print(index,value)

Operation Result:

0 I 1 is 2 cold 3 in 0.1s]

Enumerate ()--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.