table of Contents
Iterator
Iterator function is not just a call
Everything in python objects (data type)
Iterables: Data type comprising .__ iter__ method called iterables
In addition to numeric types, all data types are iterables
Iterator object: Object containing .__ iter__ and .__ next__ method is iterator object
Why should iterator object: to provide a means not dependent on the value of the index
for circulation principle (for loop is essentially a while loop, but is a certain controllable while loop)
dic_iter = dic.__iter__()
# while True:
# try:
# print(dic_iter.__next__())
# except StopIteration:
# break
Iterables use iterator object into .__ iter__
Iterator .__ iter__ still using iterators
Iterables not necessarily the iterator object, the object must be an iterator iterable
A triplet of expressions
# if x > y:
# print(x)
# else:
# print(y)
A triplet of expressions - "three head expression
print(x) if x > y else print(y) # --> 仅作了解
Go here if conditions are true condition is not satisfied the conditions else go here
List comprehensions
lt = [i**2 for i in range(10)]
from typing import Iterable # derive an iteration object type
print (isinstance (range (10), Iterable)) # determines whether the data type
Dictionary of formula
dic = {i:i**2 for i in range(10)}
zip()
# res = zip([1,2,3],[4,2,3,4,2,3,4,2,3],'abcadsfasdfasdf') # res是一个迭代器,__next__返回元组
# print(res.__next__()) # type:tuple
# print(res.__next__()) # type:tuple
# print(res.__next__()) # type:tuple
# # print(res.__next__()) # type:tuple
lt1 = ['a', 'b', 'c']
lt2 = [1, 2, 3]
dic = {k: v ** 2 for k, v in zip(lt1, lt2)}
print(dic)
lt = list('abc')
print(lt)
Generator formula
generator 本质是一个迭代器 ---> 生成器: 本质就是迭代器,生成器就是一个自定义的迭代器
g = (i for i in range(10000000))
print(g)
# for i in g:
# print(i)
lt = [i for i in range(10000000)]
print(lt)
Generator expressions: as the old hen, save memory space, use it to lay
the list comprehensions: as a basket of eggs, so take up memory space
Builder
# 生成器:含有yield关键字的函数叫做生成器
def ge():
yield 3 # 一个yield相当于一个next; 暂停函数
yield 4
# print(ge()) # ge()得到一个生成器 --> 生成器本质是迭代器
g = ge() # 得到一个生成器
yield characteristics
- Pause function
- By next value
return characteristics
- Termination function
- Get value by calling the function
Write a range method
- Builder
- Vararg
What we lack, willy-nilly, dry hands
Recursion
# 传递 ; 回归
# 递归: 函数a内部直接调用函数a本身
# import sys
# sys.setrecursionlimit(10)
# print(sys.getrecursionlimit())
#
# def a():
# x = 1
# print(x)
# a()
#
#
# a()
Each recursive, will not end function? No, and each recursive will open up memory space has been opened up if memory is blown up, so most recursive 1000
The real recursive have to have an exit condition
Recursion:
- Internal calls the function yourself
- There must be an exit condition
- Recursive must have the law