#迭代器
#dir () to see which functions are defined inside an object or class.
# Print (dir (int))
# Print (dir (list))
# tu = (1,)
# Print (dir (TU))
#__iter__ () Gets an iterator for an iterative object
LST = [1,2,3,4,5,6,7,8,9]
it = lst.__iter__ ()
#__next__ () Gets the elements in the iterator
# Print (it.__next__ ())
# Print (it.__next__ ())
# Print (it.__next__ ())
# Print (it.__next__ ())
#已到迭代器的最后一个元素再执行__next__ () will report error stopiteration.
#生成器
#1. Getting the generator by generator function
#2. Implementation of generators by various derivation formulas
#3. A generator can also be obtained by converting data
#什么是生成器?
#自己通过python写的迭代器就是生成器
#return和yield的区别
#函数中是return, a return value is generated when the function is executed, and the function is terminated directly when the function encounters a return during execution.
#函数中时yield, when executing a function, it is no longer the function's execution, but rather a generator object obtained through the function. Yield also returns a value,
#但不会终止函数, the next yield is performed by __next__ ().
def func ():
Print (111)
Yield 222
Print (333)
Yield 444
Gen = func ()
Ret0 = gen.__next__ ()
Print (RET0)
Ret1 = gen.__next__ ()
Print (RET1)
#当函数运行到最后一个yield, then execute the __next__ () program will error
The difference between #send () and __next__ ()
#send () and __next__ () will let the program perform to the next yield
The #send () can pass a value to the position of the previous yield, cannot pass the value to the last yield, cannot use Send () when the generator code is executed for the first time, but can use __next__ ()
# li = []
# for I in Range (1,14):
# li.append (' python%d period '%i)
# Print (LI)
#
# L2 = [i-I in range (1,14)]
# Print (L2)
#
# l3 = [' python%s period '%i for I in Range (1,14)]
# Print (L3)
#
# l4 = [' technician%s '%i for I in range (16)]
# Print (L4)
#
# l5 = [' Nurse%s '%i for I in Range (1,14,2)]
# Print (L5)
#可迭代对象
# Internally containing the __iter__ () method is an iterative object
# An Iterative object cannot be directly evaluated because there is no __next__ () method inside
#可迭代对象-to-generator
# obj.__iter__ ()
# iter (obj)
#迭代器的优点
# 1. Save memory
# 2. Inertia mechanism
# 3. Unidirectional irreversible
#如何判断可迭代对象
# (' __iter__ ' in Dir (obj))
# isinstance (obj,)
# LST = []
# from Collections Import iterable
# Print (Isinstance (lst,iterable))
# from Collections Import Iterator
# Print (Isinstance (lst,iterator))
#文件句柄时迭代器
#生成器表达式, List-derived
#分类: Looping mode filter mode
#循环模式: [Variable or processed variable for variable in iterable]
# LST = [I for I in range (11)]
# Print (LST)
# lst1 = [' python%s period '%i for I in Range (1,11)]
# Print (LST1)
#优点: Save code, look tall on the
#缺点: Bad Troubleshooting
#整体:
#凡是用列表推导式构造的列表对象, it can be built in other ways. A very complex list, the list derivation is not constructed.
#筛选模式: [Variable or post-processing variable for variable in iterable if condition]
# L1 = [i**2 for i in range (11)]
# print (L1)
#
# L2 = [i**2 for i in range] if I% 3 = = 0]
# Print (L2)
#
# l3 = [i-I in range (1,101,2)]
# Print (L3)
#
# l3 = [I for I in range (101) if I% 2 = = 1]
# Print (L3)
# names = [' Tom ', ' Billy ', ' Jefferson ', ' Andrew ', ' Wesley ', ' stevne ', ' Joe '],[' Alice ', ' Jill ', ' Ana ', ' Wendy ', ' Jennifer ', ' Sherry ', ' Eva ']
# lst = [El for lst1 in names for El in Lst1 if El.upper (). Count (' E ') = = 2]
# Print (LST)
python-iterators and generators