Advanced Functions - iterator

Iterator

Iteration: Iteration is upgrading, such as your grandfather gave birth to your father, your father gave birth to you, iteration can be said to be repeated, and repeated every time but are based on the results of the last. Such as iterative development of the computer is updated based on a version of the software. The following code is not iterative, it just simply repeat
the iterator: Tools iteration.
Other explanations ->
iteration is one of the most powerful features of Python is a way to access the collection elements.
Iterator can remember the object is a traverse position.

while True:
    print('*'*10)

A Dian iterables

Everything in python objects, such as

x = 1
name = 'nick'
lis = [1, 2]
tup = (1, 2)
dic = {'name': 'nick'}
s1 = {'a', 'b'}

def func():
    pass

f = open('49w.txt', 'w', encoding='utf-8)

For all objects, the object whenever there is __iter__ method, are iterable.

# x = 1.__iter__  # SyntaxError: invalid syntax

# 以下都是可迭代的对象

name = 'nick'.__iter__
lis = [1, 2].__iter__
tup = (1, 2).__iter__
dic = {'name': 'nick'}.__iter__
s1 = {'a', 'b'}.__iter__
f = open('49w.txt', 'w', encoding='utf-8')
f.__iter__
f.close()

to sum up

Iterable object: Python built-in str, list, tuple, dict, set, file are iterable.
Features:
 1. There __iter__ built-in objects can be called iterative method.


Two Dian iterator object

Only strings and lists are dependent on the value of the index, while other iterable are not dependent on the value of the index. So we have to find a way to make other iterable does not depend on the index value.

Before the method is found first of all give us the concept of iterator objects: Objects can perform iterative method return values ​​__iter__ get. And iterables have a __next__ method.

# 不依赖索引的数据类型迭代取值
dic = {'a': 1, 'b': 2, 'c': 3}
iter_dic = dic.__iter__()
print(iter_dic.__next__())
print(iter_dic.__next__())
print(iter_dic.__next__())
# print(iter_dic.__next__())  # StopIteration:

a b c

# 依赖索引的数据类型迭代取值
lis = [1, 2, 3]
iter_lis = lis.__iter__()
print(iter_lis.__next__())
print(iter_lis.__next__())
print(iter_lis.__next__())
# print(iter_lis.__next__())  # StopIteration:

1 2 3
The above method is very complicated, we can use a while loop down streamlined. Where the try ... except ... exception handling module, it will be explained in detail later.

s = 'hello'
iter_s = s.__iter__()

while True:
    try:
        print(iter_s.__next__())
    except StopIteration:
        break

h e l l o

to sum up

Iterator object: iterative execution may __iter__ object, get a return value iterator object.

Features:

 1. Built __next__ method, the method is performed will get a value iterator object
 2. built __iter__ method, the method is performed will get the iterator itself
 3. iterator object file itself.
Disadvantages:

 1. The value of trouble, can only take one by one, and only later taken, take the value gone
 2. unusable len () method to get the length

Wed and principles for loop

The for loop called the iterator loop must be in the iterative object.

lis = [1, 2, 3]
for i in lis:
    print(i)

1 2 3
After the object because the iterator __iter__ after use or iterator itself, and therefore not considered in the for loop is an iterative iterator object or objects.

Because of the iterables become a iterator object after use __iter__ method, the iterator object occupies only a small memory space, will spit out a post he only use a value __next__. As lis = [1,2,3,4,5, ...] is equivalent to a an egg, and lis = [1,2,3,4,5, ...] .__ iter__ corresponds to a mother chicken, eggs if you need to, you can just __next__.
Python2 in

print(range(10))  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Python3 in

print(range(10))  # range(0, 10)

Guess you like

Origin www.cnblogs.com/suren-apan/p/11374769.html