1, first to an example, there is a preliminary impression:
myTuple=(123,‘xyz‘,45.67)i=iter(myTuple)i.next()123i.next()‘xyz‘i.next()45.67i.nextcall""1in?StopIteration
The above code explicitly uses the iterator through the ITER () function, and the iterator is a class containing the next function, and
Usually we are using such as:
forin seq: do_something_to(i)
Also used in the iterator, the actual working code should be as follows:
fetch=iter(seq)whileTrue: try: i = fetch.next() except StopIteration: break do_something_to(i)
And, normally we should use iterators in the same way as above to wrap iterators in try...except ... In
2. Type of Use
In addition to tuples, lists, dictionaries, files, etc. can be used,
such as dictionary type
forin myDict: do_something_to(i)
The dictionary iterator iterates through its keys
3. Precautions
when iterating over mutable objects, do not attempt to modify them.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Iterators and ITER () functions