Python Iterative usages tutorial _python

Source: Internet
Author: User

This example describes the use of iterations in Python and is a very useful technique. Share for everyone to use for reference. The specific analysis is as follows:

If given a list or tuple, we can iterate through the list or tuple through a for loop, which we are iterating over (iteration).

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

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

As you can see, the python for loop is more abstract than the Java for Loop, because Python's for loop can be used not only on list or tuple, but also on other iterations.

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

>>> d = {' A ': 1, ' B ': 2, ' C ': 3}
>>> for key in D: ...   Print Key
...
A
c
b

Because Dict stores are not listed in the order of the list, the resulting order of iterations is likely to be different.

By default, the Dict iteration is the key. If you want to iterate over value, you can use the for value in D.itervalues (), and if you want to iterate both key and value, you can use for K, V in D.iteritems ().

Because strings are also iterated objects, they can also be used for A For loop:

>>> for ch in ' ABC ': ...   Print ch
...
A
B
C

So, when we use a For loop, as long as it works on an iterative object, the for loop works, and we don't care much about whether the object is a list or other 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 collections import iterable
>>> isinstance (' abc ', iterable) # str can iterate
True
>>> isinstance ([1,2,3], iterable) # list can iterate
True
>>> isinstance (123, iterable) # integers can be iterated
False

Last small question, what if you want to implement a subscript loop like Java for the list? Python's built-in enumerate function can turn a list into an index-element pair so that the index and the elements themselves can be iterated in the for loop at the same time:

>>> for I, value in enumerate ([' A ', ' B ', ' C ']):
...   Print I, value
...
0 A
1 B
2 C

In the For loop above, it is common in Python to reference two variables, such as the following code:

>>> for x, y in [(1, 1), (2, 4), (3, 9)]:
...   Print x, y
...
1 1
2 4
3 9

Summary:

Any iteration object can be used for a for loop, including our custom data type, which can be used with the for loop as long as the iteration condition is met.

I hope the examples in this article about iterations are helpful to the learning of Python programming.

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.