Iteration
Given a list or tuple, we can iterate through for
the list or tuple through a loop, This traversal is called iterative (iteration).
Iterative: A sequence of objects, physical, or virtual storage.
List,tuple,strings,dictionary,set and generator objects are iterative, and integer types are not iterative. So, how can you tell if an object is an iterative object? The method is judged by the iterable type of the collections module:
" in Python, you can use the type () and isinstance () functions to determine the object type. 】
Or is:
iter () returns the listiterator object for the list , returning the dictionary-keyiterator for dictionary Object. Similarly, an iterator object is returned for other types of iterations.
The last small question, what if I want to implement a Java -like subscript loop on the list ? Python 's built-in enumerate
function allows you to turn a list into an index-element pair so that you can for
iterate over both the index and the element in the loop Itself:
Iterators
An object that can be called by the Next ()
function and continually returns the next value is called an iterator:Iterator.
You can use isinstance () to determine whether an object is a Iterator object:
Generators are Iterator objects, but list
dict
str
, although iterable, they are not Iterator.
Iterable, and so on into Iterator can use iter()
the function: list
dict
str
Generator
In Python , a mechanism that loops one side of the computation, called the generator:generator.
Create a generator:
The L
difference g
between creation and the outermost []
and ()
L
is a list, and g
is a generator.
"generator is the algorithm "
iteration. Iterator. Builder. Python