The Python iteration

Source: Internet
Author: User
Tags iterable

1. What is an iteration:

For a given list or tuple, traverse the list or tuple through a for loop, which we call iteration (iteration).

use the for...in statement in 2.python to complete the iteration statement:(1) Iteration list:

When iterating through the list using the for...in statement, it is done by subscript, and the iteration tuple is similar

>>>months=['January','February','  March']>>> for in range (len (months)):         Print(months[i]) Januaryfebruarymarch
(2) Iterative dict:

First, it is clear that the list of data types is subscript, but like Dict, which has no subscript data type, can also be iterated, as long as the data type is an iterative object, such as the iterative Dict method is as follows:

>>>d = {' xiaoming ': 0,' Little Red ': $,' xiao Lan ' : A-z }>>> for in D:       print(key)
--------------------------------------------------------------------------------Ming Minglan Little Red

Note: A. Because Dict storage is not ordered in list order, the resulting order of the iterations is likely to be different.

B.dict iteration, the key that iterates by default, if you want to iterate over value can be used for value in D.values (),

If you want to iterate both key and value at the same time, you can use for K and V in D.items ()

Iteration Value Case:

>>>d = {' xiaoming ': 0,' Little Red ': $,' xiao Lan ' : A-z }>>> for value in    d.values (       ):Print  (value)-----------------------------------------------------------------------------------65 088

Simultaneously iterate key and value cases:

>>>d = {' xiaoming ': 0,' Little Red ': $,' xiao Lan ' : A-z }>>> for-K, V in    d.items       ():Print (k,v)-----------------------------------------------------------------------------------  88
(3) Iterate string

Because a string is also an iterative object, we can iterate over the string:

>>> for in'ABC':       print(CH)
-------------------------------------------------------------------------------------
A
B
C

3. Use the iterable type of the collections module to determine whether an object is an iterative object

Usually when we use a For loop, as long as the object is an iterative object, the for loop will work, not the data type of the object, but how do we know if the object is an iterative object, which we need to judge by ourselves, The method is judged by the iterable type of the collections module.

 from Import iterable>>> isinstance ('abc'#  str can iterate True  #  Whether the list can iterate True#  integer Whether it can iterate False

4. Using the Python built-in function enumerate function同时迭代list索引及元素本身

>>> for in enumerate (['January','February ','March']):           print(i,value)------- ---------------------------------------------------------------------0 January1 February2 March

PS: The FOR Loop statement above refers to two variables, which is very common in Python, for example:

 for inch [(1, 2), (2, 4), (3, 8)]: ...      Print (x, y) ... ---------------------------------------------------------------1 22 43 8

  

The Python iteration

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.