Python: Iteration

Source: Internet
Author: User

Given a list or tuple, we can iterate through for the list or tuple through a loop, which we call an iteration (iteration).

In Python, iterations are for ... in done through, and in many languages such as C, the iteration list is done by subscript, such as Java code:

 for (i=0; i<list.length; i++) {    = list[i];}

As you can see, Python's for loop is more abstract than the C for loop, because the Python for loop can be used not only on a list or a tuple, but also on other objects that can iterate.

List this data type although there is subscript, but many other data types are not subscript, but as long as the object can be iterated, whether or not subscript, can iterate, such as dict can iterate:

>>> d = {'a'b' 'C': 3 } for in D: ...      Print (Key) ... ACB

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

By default, the Dict iteration is key. If you want to iterate over value, you can use for value in d.values() it if you want to iterate both key and value at the same time for k, v in d.items() .

Because a string is also an iterative object, it can also be used for for loops:

 for inch ' ABC ' :...      Print (CH) ... ABC

So, when we use for a loop, the loop works as long as it works on an iterative object, for and we don't care much about whether the object is a list or another data type.

So, how can you tell if an object is an iterative object? 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

The last small question, what if I want to implement a Java-like subscript loop on the list? Python's built-in enumerate functions can turn a list into an index-element pair so that the for index and the element itself can be iterated at the same time in the loop:

 for  in Enumerate (['A''B''C' ]):...      Print (i, value) ... 0 A1 B2 C

The above for loop, which references two variables at the same time, is common in python, such as the following code:

 for inch [(1, 1), (2, 4), (3, 9)]: ...      Print (x, y) ... 1 12 43 9

Python: Iteration

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.