#!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