Classroom yield 3.25 recursive function calls a triplet of expressions expression

yield a triplet of expressions Expressions

 

yield expression

# x=yield 返回值
# def dog(name):
# print('道哥%s准备吃东西啦...' %name) # while True: # # x拿到的是yield接收到的值 # x = yield # x = '肉包子' # print('道哥%s吃了 %s' %(name,x)) # # # g=dog('alex') # g.send(None) # 等同于next(g) # # g.send(['一根骨头','aaa']) # # g.send('肉包子') # # g.send('一同泔水') # # g.close() # # g.send('1111') # 关闭之后无法传值 

A triplet of expressions

Syntax: The value to return value if condition else condition if the conditions are not satisfied to be returned

x=1
y=2

# res=x if x > y else y
# print(res)


res=111111 if 'egon' == 'egon' else 2222222222
print(res)

Generative

# 1、列表生成式
l = ['alex_dsb', 'lxx_dsb', 'wxx_dsb', "xxq_dsb", 'egon']
# new_l=[]
# for name in l: # if name.endswith('dsb'): # new_l.append(name)  # new_l=[name for name in l if name.endswith('dsb')] # new_l=[name for name in l]  # print(new_l)  # 把所有小写字母全变成大写 # new_l=[name.upper() for name in l] # print(new_l)  # 把所有的名字去掉后缀_dsb # new_l=[name.replace('_dsb','') for name in l] # print(new_l)  # 2、字典生成式 # keys=['name','age','gender'] # dic={key:None for key in keys} # print(dic)  # items=[('name','egon'),('age',18),('gender','male')] # res={k:v for k,v in items if k != 'gender'} # print(res)  # 3、集合生成式 # keys=['name','age','gender'] # set1={key for key in keys} # print(set1,type(set1))  # 4、生成器表达式 # g=(i for i in range(10) if i > 3) # !!!!!!!!!!!强调!!!!!!!!!!!!!!! # 此刻g内部一个值也没有  # print(g,type(g)) # print(next(g)) # print(next(g)) 

Application generator expressions

with open('笔记.txt', mode='rt', encoding='utf-8') as f: # 方式一: # res=0 # for line in f: # res+=len(line) # print(res) # 方式二: # res=sum([len(line) for line in f]) # print(res) # 方式三 :效率最高 # res = sum((len(line) for line in f)) # 上述可以简写为如下形式 res = sum(len(line) for line in f) print(res)

Recursive function calls

 

A recursive definition

The recursive function: the function is a special form of nested calls, in particular: a function calling process has to call itself directly or indirectly

# 直接调用本身
# def f1():
# print('是我是我还是我') # f1() # f1()  # 间接接调用本身 # def f1(): # print('===>f1') # f2() # # def f2(): # print('===>f2') # f1() # # f1()  # 一段代码的循环运行的方案有两种 # 方式一:while、for循环 # while True: # print(1111) # print(2222) # print(3333)  # 方式二:递归的本质就是循环: # def f1(): # print(1111) # print(2222) # print(3333) # f1() # f1() 

Second, the need to emphasize

Infinitely recursive call should not call it, must end in a recursive call to meet certain conditions

# def f1(n):
#     if n == 10: # return # print(n) # n+=1 # f1(n) # # f1(0) 

Two-stage recursive

Back: call down layer by layer

Recursive: to meet certain conditions of the end, the end of the recursive call, and then return one level

# age(5) = age(4) + 10
# age(4) = age(3) + 10
# age(3) = age(2) + 10 # age(2) = age(1) + 10 # age(1) = 18  # def age(n): # if n == 1: # return 18 # return age(n-1) + 10 # # # res=age(5) # print(res) 

Applied recursively

l=[1,2,[3,[4,[5,[6,[7,[8,[9,10,11,[12,[13,]]]]]]]]]] def f1(list1): for x in list1: if type(x) is list: # 如果是列表,应该再循环、再判断,即重新运行本身的代码 f1(x) else: print(x) f1(l)

yield expression

# x=yield 返回值
# def dog(name):
# print('道哥%s准备吃东西啦...' %name) # while True: # # x拿到的是yield接收到的值 # x = yield # x = '肉包子' # print('道哥%s吃了 %s' %(name,x)) # # # g=dog('alex') # g.send(None) # 等同于next(g) # # g.send(['一根骨头','aaa']) # # g.send('肉包子') # # g.send('一同泔水') # # g.close() # # g.send('1111') # 关闭之后无法传值 

A triplet of expressions

Syntax: The value to return value if condition else condition if the conditions are not satisfied to be returned

x=1
y=2

# res=x if x > y else y
# print(res)


res=111111 if 'egon' == 'egon' else 2222222222
print(res)

Generative

# 1、列表生成式
l = ['alex_dsb', 'lxx_dsb', 'wxx_dsb', "xxq_dsb", 'egon']
# new_l=[]
# for name in l: # if name.endswith('dsb'): # new_l.append(name)  # new_l=[name for name in l if name.endswith('dsb')] # new_l=[name for name in l]  # print(new_l)  # 把所有小写字母全变成大写 # new_l=[name.upper() for name in l] # print(new_l)  # 把所有的名字去掉后缀_dsb # new_l=[name.replace('_dsb','') for name in l] # print(new_l)  # 2、字典生成式 # keys=['name','age','gender'] # dic={key:None for key in keys} # print(dic)  # items=[('name','egon'),('age',18),('gender','male')] # res={k:v for k,v in items if k != 'gender'} # print(res)  # 3、集合生成式 # keys=['name','age','gender'] # set1={key for key in keys} # print(set1,type(set1))  # 4、生成器表达式 # g=(i for i in range(10) if i > 3) # !!!!!!!!!!!强调!!!!!!!!!!!!!!! # 此刻g内部一个值也没有  # print(g,type(g)) # print(next(g)) # print(next(g)) 

Application generator expressions

with open('笔记.txt', mode='rt', encoding='utf-8') as f: # 方式一: # res=0 # for line in f: # res+=len(line) # print(res) # 方式二: # res=sum([len(line) for line in f]) # print(res) # 方式三 :效率最高 # res = sum((len(line) for line in f)) # 上述可以简写为如下形式 res = sum(len(line) for line in f) print(res)

Recursive function calls

 

A recursive definition

The recursive function: the function is a special form of nested calls, in particular: a function calling process has to call itself directly or indirectly

# 直接调用本身
# def f1():
# print('是我是我还是我') # f1() # f1()  # 间接接调用本身 # def f1(): # print('===>f1') # f2() # # def f2(): # print('===>f2') # f1() # # f1()  # 一段代码的循环运行的方案有两种 # 方式一:while、for循环 # while True: # print(1111) # print(2222) # print(3333)  # 方式二:递归的本质就是循环: # def f1(): # print(1111) # print(2222) # print(3333) # f1() # f1() 

Second, the need to emphasize

Infinitely recursive call should not call it, must end in a recursive call to meet certain conditions

# def f1(n):
#     if n == 10: # return # print(n) # n+=1 # f1(n) # # f1(0) 

Two-stage recursive

Back: call down layer by layer

Recursive: to meet certain conditions of the end, the end of the recursive call, and then return one level

# age(5) = age(4) + 10
# age(4) = age(3) + 10
# age(3) = age(2) + 10 # age(2) = age(1) + 10 # age(1) = 18  # def age(n): # if n == 1: # return 18 # return age(n-1) + 10 # # # res=age(5) # print(res) 

Applied recursively

l=[1,2,[3,[4,[5,[6,[7,[8,[9,10,11,[12,[13,]]]]]]]]]] def f1(list1): for x in list1: if type(x) is list: # 如果是列表,应该再循环、再判断,即重新运行本身的代码 f1(x) else: print(x) f1(l)

A recursive definition

The recursive function: the function is a special form of nested calls, in particular: a function calling process has to call itself directly or indirectly

# 直接调用本身
# def f1():
# print('是我是我还是我') # f1() # f1()  # 间接接调用本身 # def f1(): # print('===>f1') # f2() # # def f2(): # print('===>f2') # f1() # # f1()  # 一段代码的循环运行的方案有两种 # 方式一:while、for循环 # while True: # print(1111) # print(2222) # print(3333)  # 方式二:递归的本质就是循环: # def f1(): # print(1111) # print(2222) # print(3333) # f1() # f1() 

Second, the need to emphasize

Infinitely recursive call should not call it, must end in a recursive call to meet certain conditions

# def f1(n):
#     if n == 10: # return # print(n) # n+=1 # f1(n) # # f1(0) 

Two-stage recursive

Back: call down layer by layer

Recursive: to meet certain conditions of the end, the end of the recursive call, and then return one level

# age(5) = age(4) + 10
# age(4) = age(3) + 10
# age(3) = age(2) + 10 # age(2) = age(1) + 10 # age(1) = 18  # def age(n): # if n == 1: # return 18 # return age(n-1) + 10 # # # res=age(5) # print(res) 

Applied recursively

l=[1,2,[3,[4,[5,[6,[7,[8,[9,10,11,[12,[13,]]]]]]]]]] def f1(list1): for x in list1: if type(x) is list: # 如果是列表,应该再循环、再判断,即重新运行本身的代码 f1(x) else: print(x) f1(l)

Guess you like

Origin www.cnblogs.com/haliluyafeng/p/12570814.html