This article mainly introduces the loop-related functions range, enumerate, and zip in the python advanced tutorial. These functions are often used with loop programs to complete loops, for more information, see the "loop" section. We have discussed the basic loop Syntax of Python. In this section, we will be exposed to more flexible loop methods.
Range ()
In
Python standard library: built-in function enumerate (iterable, start = 0), enumerateiterable
This function converts an Iterated object to an enumerated object. Iterable is an iterative parameter, such as a list, array, or dictionary object. start is the start value of enumeration. The default value is from 0. The implementation principle of this function is as follows: Get a value from the method _ next _
The enumerate () function iterates over an index of a data object that can be traversed (such as a list, tuple, or string) and its corresponding elements, typically used in a for loop.Enumerate (sequence, [start=0])Sequence represents a sequence, iterator, or other supporting iteration object; Start represents the starting position of the index. Note that the start index has a starting position unrelated to sequence. The function return value is a
In Python applications, when we use an array or a list to traverse the index and iterate over the elements, it is common practice to:>>> LSI = [1,2,3,4,5,6,7,8,9]>>> lsi[1, 2, 3, 4, 5, 6, 7, 8, 9]for
in
Range (0,len (LSI)): ...
Print
Using the built-in enumerrate function will have a more straightforward, graceful approach, first look at the definition of enumerate :def
This example describes the use of enumerate function traversal elements in Python. Share to everyone for your reference, as follows:
The enumerate function iterates through the elements in the sequence and their subscripts
The sample code is as follows:
i = 0seq = [' One ', ' II ', ' three ']for element in seq: print I, seq[i] i + = 1#0 one#1 two#2 threepri
In other languages, for example, C #, we usually use the following method to traverse Arrays:
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->
For
(
Int
I
=
0
; I
List. length; I
++
)
{//Todo with list [I]}
In python, we get used to traversing like this:
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->ForItemInSequence:Pro
The enumerate function iterates through the elements in the sequence and their subscripts:>>> for I,j in Enumerate ((' A ', ' B ', ' C ')):Print I,j2 #1 b2 C>>> for I,j in enumerate ([+]):Print I,j0 10 S2 3>>> for I,j in enumerate ({' A ': 1, ' B ': 2}):Print I,j2 #1 b>>> for I,j in
Python built-in functions (18) -- enumerate, pythonenumerate
English document:
enumerate(Iterable,Start = 0)
Return an enumerate object.IterableMust be a sequence, an iterator, or some other object which supports iteration.__next__()Method of the iterator returnedenumerate()Returns a tuple containing a count (fromStart
Enumerate enumerationenumerate(iterable, start=0)Return an Enumerate object. iterable must is a sequence, an iterator, or some other object which supports iteration. The __next__() method of the iterator returned by enumerate() returns a tuple containing a count (from start which defaults t o 0) and the values obtained from iterating over iterable.Converts an obj
Python learning notes enumerate () and range (len) usage and assignment subtotal, pythonenumerate
#! /Uer/bin/env python # _ * _ coding: UTF-8 _ * _ # format 1a = 'abc' for I in range (len ()): print a [I], '(% d)' % I
A (0)B (1)C (2)
# Format 2for A, I in enumerate ('abc'): print I,
A 0B 1C 2
# Format 2.1b = raw_
Many times I need to use the function of the Python list, such as the need to use the list of each element of the data and index, I used to set a variable to save the index, now good, yesterday just saw enumerate () function, only hate myself did not find this function earlier, too good, below say:For index, the data in enumerate ([' A ', ' B ', ' C '):Print Inde
In Python, we are accustomed to traverse this way:650) this.width=650; "src=" Http://www.cnblogs.com/Images/OutliningIndicators/None.gif "align=" Top "/> for Item in sequence:650) this.width=650; "src=" Http://www.cnblogs.com/Images/OutliningIndicators/None.gif "align=" Top "/> Print (item)This iterates through the item's ordinal I, all with the following traversal method:650) this.width=650; "src=" Http://www.cnblogs.com/Images/OutliningIndicators
#代码是在3.5 of the interpreter running under theA = ["my", "name", "is", "XXX"]#以 (Index,value) in the form of a line printFor index in range (Len (a)): #遍历索引下标 Print (Index,a[index]) #同时输出索引, and the value of the variable#输出结果:#0 my#1 Name#2 is#3 xxx#enumerate的用法起相同的作用For Index,value in Enumerate (a): Print (Index,value)#输出结果:#0 my#1 Name#2 is#3 xxx#在列表中输出def read_index_value (Index,value): Ret
In the "Looping" section, we've discussed Python's basic looping syntax. In this section, we will be exposed to a more flexible way of cycling.
Range ()
In Python, the For loop follows a sequence in which the sequence elements are used each time, instead of the subscript of the sequence.
We've used range () before to control the for loop. Now we continue to develop the range feature to enable the subscript to control the loop:
Copy Code code
This is a Python built-in function that found him while reading a book, Mark.When we need to traverse the index and iterate through the elements, we can consider using the enumerate function, the enumerate function accepts a traversal object, such as a list, a string
For example, we have a list of ["One", "two", "there"], and we need to precede each element of t
>>>filter (f, D)4[1, 2, 3]5>>> Map (LambdaX:x>2, D)6 [False, False, True]7>>>defF1 (s):8... s=s*1209 ...Ten>>>Map (F1, D) One[None, none, none]4. zip ([iterable, ...])Zip () is an intrinsic function of Python that takes a series of iterated objects as parameters, packages the corresponding elements in the object into tuple (tuples), and then returns a list of these tuples. If the length of the passed parameter is not equal, the length of the returned
The enumerate () function is used to combine a data object that can be traversed (such as a list, tuple, or string) into an index sequence.Indexes and data are listed at the same time, typically used in a for loop.Enumerate(sequence, [start=0]) Sequence--a sequence, iterator, or other supporting iteration object.Start-the subscript start position.#------------------------List-----------------------Li = ['Alex','Eric','Rain'] forIinchEnumerate (
Http://www.cnblogs.com/linjiqin/p/4228896.htmlThe enumerate function iterates through the elements in the sequence and their subscriptsi = 0seq = [' One ', ' II ', ' three ']for element in seq: print I, seq[i] i + = 1#0 one#1 two#2 threeprint ' =========== = ' seq = [' One ', ' II ', ' three ']for I, element in enumerate (seq): print I, seq[i] print ' ============ ' for i,j in EN Umerate (' abc
1. Print the index number and its corresponding elements in the list in normal circumstancesUse the following loop:>>> L = [' A ', ' B ', ' C ', ' d ']>>> for i ' L: ... print l.index (i), I ... 0 A1 B2 C3 d
2. Use enumerate to access the index at the same time in the loopYou can use enumerate to implement the above features:>>> L = [' A ', ' B ', ' C ', ' d ']>>>
One, the string s = ' python ' s1 = ' python ' + ' learn ' #相加其实就是简单拼接s2 = ' python ' * aa multiplication is actually how many times you copy yourself, and then stitch together a string slice: a= ' abcdefg ' Print (a[0]) # Index The subscript is the string consisting of elements starting with the first, the initial index is 0 and so on. Print (a[0:]) #默认到最后print
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.