Home > Backend Development > Python Tutorial > Detailed example of iterator and iter() function

Detailed example of iterator and iter() function

Y2J
Release: 2017-05-04 14:07:21
Original
1630 people have browsed it

This article mainly introduces the detailed explanation and examples of python iterators and iter() functions. Friends in need can refer to the following

Iterators and iter() functions in python

The iterator provides a sequence-like interface for the class sequence object . Python's iteration seamlessly supports sequence objects, and it also allows programmers to iterate over non-sequence types, including user-defined objects. Iterators are very neat to use. You can iterate over objects that are not sequences but represent the behavior of a sequence, such as dictionary keys, lines of a file, etc. The functions of iterators are as follows:

•Provides an extended iterator interface;
•Brings performance enhancements to list iteration;
•Performance improvements in dictionary iteration;
•Create a true iteration interface instead of the original random object access;
•Backwards compatible with all existing user-defined classes and extended objects that simulate sequences and maps;
•Iterate over non-sequence collections (such as maps and files), you can create more concise and readable code

#iter and generator
#the first try
#=================================
i = iter('abcd')
print i.next()
print i.next()
print i.next()

s = {'one':1,'two':2,'three':3}
print s
m = iter(s)
print m.next()
print m.next()
print m.next()
Copy after login

D:\Scirpt\Python\Python AdvancedProgramming>python ch2_2.py

a
b
c
{'three': 3, 'two': 2, 'one': 1}
three
two
one
Copy after login


【Related Recommendations】

1. Free Basic Tutorial on Python

2. Python Learning Manual

3. Python meets data collection video tutorial

The above is the detailed content of Detailed example of iterator and iter() function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template