The python iterator function

5.9 iterator

5.91 iterables and iterator object

1. What is iteration? : Iteration is an iterative process, and each repetition are based on results from a

2, in order to understand the iterator in the end what is? We must first understand the concept that what is iterative target? Objects may be iterative: in python, whenever built ' ' ITER ' ' object method, an object is iterable

3, iterator: iterative value tools, can be an iterative object execution iter method return value obtained iterator object

iter_dic=dic.__iter__()
print(iter_dic.__next__())
print(iter_dic.__next__())

4, iterative target vs iterator object?

Objects may be iterative: STR, List, tuple, dict, SET, File ( except value) 
# str1 = 'Hello'
# List1 = [l, 2,3]
# TUP1 = (l, 2,3)
# = {DIC 'X':. 1}
# {S1 = 'A', 'B', 'C'}
# = F Open ( 'a.txt', 'W', encoding = 'UTF-. 8') iterator object: file the object itself is iterator object
1 , get iterables way: no need to get, python built-str, list, tuple, dict, set, file are iterables
2 , features: built-in method can be called __iter__ iterative object, the method is performed will get built iterator object 1 , iterator object acquired by: performing an iterative method __iter__ object, get a return value iterator object 2 , features: both built _ _iter__ method, and a built-__next__ method of execution iterator object __iter__ still get the iterator itself at a value iterator execution iterator object __next__ obtained for Item in iterable objects: pass
   


   

   
   
   

               

Advantages and disadvantages of 5.92 iterator

Iterator advantages: 1, a value of the index may depend on the manner

l=open('a.txt','r',encoding='utf-8')
iter_l=l.__iter__()
while True:
try:
        print(iter_l.__next__())
    except StopIteration:
        break
L = [ 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6] 
iter_l = l. __iter __ ()
Print ( iter_l)
Print ( iter_l. __next __ ())

2, iterators save more memory

item=range(0,100000000000000000000000000000000000000000000)
print(item)

Shortcoming iterator: 1, the value of trouble, can only take one by one, only to later take, 2, can only take back, one-time, take complete throw a StopIteration, you can not get with len length

x=[1,2,3]
iter_x=x.__iter__()
while True:
    try:
        print(iter_x.__next__())
    except StopIteration:
        break
print('第二次=================================》')
iter_x=x.__iter__()
while True:
    try:
        print(iter_x.__next__())
    except StopIteration:
        break

5.93 for loop principle analysis:

1, for loop iterator called loop, must be followed in an iterative object 2, for loop is executed in the object iter method, get iterator object 3, and then calls the iterator object next method, take a return value to assign to the Line, perform a loop 4, again and again, until the value is completed, the abnormality is detected for loop will automatically end loop

l=open('a.txt','r',encoding='utf-8')
for line in l: #iter_l=l.__iter__()
    print(line)

for item in {'x':1,'y':2}:
    print(item)

Guess you like

Origin www.cnblogs.com/mylu/p/11012985.html