Python iteration usage example tutorial, python usage example tutorial
This article describes how to use iterations in Python. It is a very practical technique. Share it with you for your reference. The specific analysis is as follows:
If a list or tuple is given, we can traverse the list or tuple through the for loop. This traversal becomes an Iteration ).
In Python, iterations are completed through for... in. in many languages such as C or Java, iterative list is done by subscript, such as Java code:
for (i=0; i<list.length; i++) { n = list[i];}
It can be seen that Python's for loop is more abstract than Java's for loop, because Python's for loop can be used not only on list or tuple, but also on other iteratable objects.
Although the list data type has a subscript, many other data types do not have a lower standard, but as long as it is an iteratable object, it can be iterated regardless of whether there is a subscript, such as dict can be iterated:
>>> d = {'a': 1, 'b': 2, 'c': 3}>>> for key in d:... print key...acb
Because the storage of dict is not arranged in the order of list, the order of iteration results may be different.
By default, key is iterated by dict. If you want to iterate value, you can use for value in d. itervalues (). If you want to iterate both key and value, you can use for k, v in d. iteritems ().
Because the string is also an iteratable object, it can also act on the for Loop:
>>> for ch in 'ABC':... print ch...ABC
Therefore, when we use a for loop, as long as we act on an iteratable object, the for loop can run normally, and we are not very concerned about whether the object is a list or other data type.
So how can we determine that an object is an iteratable object? The method is determined by the Iterable type of the collections module:
>>> From collections import Iterable >>> isinstance ('abc', Iterable) # Can str iterate True >>> isinstance ([1, 2, 3], Iterable) # Whether the list can be iterated True >>> isinstance (123, Iterable) # Whether the integer can be iterated False
What if I want to implement a subscript loop similar to Java for list? Python's built-in enumerate function can convert a list into an index-element pair, so that the index and element itself can be iterated simultaneously in the for Loop:
>>> for i, value in enumerate(['A', 'B', 'C']):... print i, value...0 A1 B2 C
In the for loop above, two variables are referenced at the same time, which is very common in Python, such as the following code:
>>> for x, y in [(1, 1), (2, 4), (3, 9)]:... print x, y...1 12 43 9
Summary:
Any iteratable object can act on the for loop, including the custom data type. As long as the iteration conditions are met, the for loop can be used.
I hope that the iterative examples described in this article will help you learn Python programming.
How to Use the iterator in python?
List = [1, 2, 3, 4, 5, 6]
For item in list:
Print item
How to Use the iterator in python? For example,
Are you talking about enumerate?
The simplest example is as follows:
ListValue = ["a", "B", "c"]
For index, strValue in enumerate (listValue ):
Print index, "is", strValue
The result is:
0 is
1 is B
2 is c