# 生成器三个方法:send,close throw# send()发送数据 f# def gen():# for i in range(10):# j=yield i# print(j)# g=gen()## print(next(g))# print(g.send(100))# print(next(g))# print(next(g))# print(g.send(10000))# print(next(g))## close()关闭生成器# g.close()# print(g.send(100)) # 关闭生成器后,无法返回值 # throw() 异常方法 ---->不常用# g.throw(Exception, "Method throw called!") # 异常类型# g.throw(ValueError, "hello python!") # 异常信息 1. 递归函数 :自己调用自己,建议不要经常用,性能差# def func():# print("9999")# func()# func()print("===========")# 阶乘函数def func(n): if n==1: # 递归临界点:不再调用自身函数条件 return 1 else: return n*func(n-1) res=func(2)print(res)
from collections import Iterator, Iterable, Generator 2. 内置函数:map filter zip # map# filter,过滤函数,第一个参数是函数,第二课参数是可迭代对象def func(n): return n > 10 li = [1, 2, 3, 444, 77, 99, 102, 345]res = filter(func, li)# 返回一个对象print(res) # <filter object at 0x10e58d5c0> print(list(res)) li2 = iter(li)li3 = (i for i in range(5)) print(isinstance(li, Iterable)) # Trueprint(isinstance(li2, Iterable)) # Trueprint(isinstance(li3, Iterable)) # True # map:将可迭代对象汇总的数据迭代出来,一个一个传到函数中去调用,将返回结果放到列表中 res2 = map(func, li)# 返回一个对象print(res2) # <map object at 0x10666a3c8>print(list(res2)) # [False, False, False, True, True, True, True, True] # zip 打包,将可迭代对象作为参数,将对象中对应的元素打包成一个个元组res4 = zip([1, 2, 3], [11, 22, 33])print(res4) # <zip object at 0x10466e7c8>print(list(res4)) # [(1, 11), (2, 22), (3, 33)] dict1={"key1":1, "key2":2, "key3":3}print(list(dict1.items())) # [(‘key1‘, 1), (‘key2‘, 2), (‘key3‘, 3)] # 匿名函数def func4(a, b): c=a+b print(c) return a+b # pep8 不介意怎么用,占用内存l1= lambda a,b:a+b# 这个写法跟下面一样的#l1= (lambda a,b:a+b)(9,8)print(l1(3,5)) # lambda 的用法li = [1, 2, 3, 444, 77, 99, 102, 345]res3=filter(lambda x:x<10,li)print(list(res3)) #[1, 2, 3] li5=[lambda x:x%5==0 for i in range(100)]print(li5) #返回函数 # 三目运算符a=100if a>100: print(100)else: print(22) # 三目运算符print(100) if a>100 else print(22) # 偏函数from functools import partialls1 = [1, 2, 3, 444, 77, 99, 102, 345]ls2 = [1, 2, 3, 444, 77, 99, 102, 345]ls3 = [1, 2, 3, 444, 77, 99, 102, 345]ls4 = [1, 2, 3, 444, 77, 99, 102, 345] filter2 =partial(filter, lambda x:x>5)res5=filter2(ls1)res5=filter2(ls2)res5=filter2(ls3)res5=filter2(ls4)print(list(res5)) # [444, 77, 99, 102, 345]
# 作业一:利用递归函数实现斐波那契数列def fib_seq(n): a,b=0,1 for i in range(n+1): a,b =b,a+b return a for i in range(20): print(fib_seq(i), end=‘ ‘) print("-----------")# 递归法def fib_loop(n): assert n>=0, "n>0" if n<=1: return n return fib_loop(n-1)+fib_loop(n-2)for i in range(1,20): print(fib_loop(i), end=" ") print("-----------")# 作业二:第三个月起每个月生一对兔子,小兔子涨到第三个月后每个月又生一对兔子,例如兔子都不死,问每个月的兔子总数生多少?(意味着生长期为:2)def rabbit_number(month): if month == 1: return 1 elif month == 2: return 1 elif month == 3: return 2 else: return rabbit_number(month - 1) + rabbit_number(month - 2)print(‘兔子总数为{}对‘.format (rabbit_number(20))) # 作业三:小明有100元钱,打算买100本书,A类书籍5元1本,B类书籍3元1本,C类书籍1元2本,请用程序算出小明一共有多少种买法def Buy_book(money, book): num = 0 for a in range(int(money / 5)): for b in range(int(money / 3)): for c in range(int(book + 1)): if a * 5 + b * 3 + c * 0.5 <= book and a + b + c == book: print(‘5元书买{}本,3元书买{}本,1元书买{}本‘.format(a, b, c)) num += 1 return numprint(‘100元钱 买100本书 共有{}中方法‘.format(Buy_book(100,100)))#结果:100元钱 买100本书 共有129中方法 # 作业四:现在有一个列表 li = [11,21,4,55,6,67,123,54,66,9,90,56,34,22], 请将 大于5的数据过滤出来,然后除以2取余数,li = [11,21,4,55,6,67,123,54,66,9,90,56,34,22]def judge(n): return n > 5 def judge2(n): return n % 2 def judge3(li): return map(judge2, list(filter(judge, li)))print(list(judge3(li))) # 作业五:有一个列表 li2 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],请用上课讲的知识处理成下面的格式:# li4 = [[1,2,3],[4,5,6],[7,8,9][10,11,12],[13,14,15]] li2 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]def conversion(li2): li4 = iter(li2) li3 = list(zip(li4, li4, li4)) h = 0 for i in li3: li3[h] = list(i) h+=1 print(li3) a = [1,2,3]b = [‘a‘, ‘b‘, ‘c‘]print(list(zip(b, a))) conversion(li2)
原文地址:https://www.cnblogs.com/vivian0119/p/12019795.html
时间: 2025-01-07 12:34:56