Home > Article > Backend Development > What are iteration and iteration objects in python?
In the following article, we will learn about iteration in python. Understand what iteration means and what role it can play in python programming.
What is iteration in python
If a list or tuple is given, we can traverse the list or tuple through a for loop, this This kind of traversal is called iteration (Iteration).
(In Python, iteration is done through for...in)
Python’s for loop is more abstract than C’s for loop because Python's for loop can be used not only on lists or tuples, but also on otheriterable objects.
(Objects that can be directly used in for loops are collectively called iterable objects (Iterable), such as list, tuple, dict, set, str, etc.)
list Although this data type has subscripts, many other data types do not have subscripts. However, as long as it ispython's iterable object, it can be iterated regardless of whether it has a subscript or not. , for example, dict can be iterated:
>>> d = {'a': 1, 'b': 2, 'c': 3} >>> for key in d:... print(key) ... a c bBecause dict is not stored in the order of a list, the order of iterated results is likely to be different. By default, dict iterates over key. If you want to iterate value, you can use for value in d.values(). If you want to iterate key and value at the same time, you can use for k, v in d.items(). Since strings are also iterable objects, they can also be used in for loops:
>>> for ch in 'ABC': ... print(ch) ...ABCSo, when we use for loops, as long as they act on an iterable object, the for loop It can run normally, and we don't care much whether the object is a list or another data type. So, how to determine whether an object is an iterable object? The method is to judge by the Iterable type of the collections module:
>>> from collections import Iterable >>> isinstance('abc', Iterable) # str是否可迭代 True >>> isinstance([1,2,3], Iterable) # list是否可迭代 True >>> isinstance(123, Iterable) # 整数是否可迭代 FalseThe last little question, what if you want to implement a subscript loop similar to 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 element itself can be iterated at the same time in a for loop:
>>> for i, value in enumerate(['A', 'B', 'C']): ... print(i, value) ... 0 A 1 B 2 CIn the for loop above, two variables are referenced at the same time, It is very common in Python, such as the following code:
>>> for x, y in [(1, 1), (2, 4), (3, 9)]: ... print(x, y) ... 1 1 2 4 3 9The above is all the content described in this article. This article mainly introduces the iteration and iteration objects in
python knowledge, I hope you can use the information to understand the above content. I hope what I have described in this article will be helpful to you and make it easier for you to learn python.
For more related knowledge, please visit thePython tutorial column on the php Chinese website.
The above is the detailed content of What are iteration and iteration objects in python?. For more information, please follow other related articles on the PHP Chinese website!