zoukankan      html  css  js  c++  java
  • 生成器应用及知识推广

    # def fn():
    #     print("盲僧")
    #     yield "瞎子"
    #     print("狮子狗")
    #     yield "辛巴"
    #     print("螳螂")
    #     yield "150斤的肥螳螂"
    #     print("冰晶凤凰")
    #     yield "冰鸟"
    # g=fn()
    # print(list(g))
    # print(g.__next__())
    # print(g.__next__())
    # print(g.__next__())
    # print(g.__next__())
    # def fn():
    #     lst=[]
    #     for i in range (1,146):
    #         lst.append("英雄联盟第%s位英雄"%i)
    #     return lst
    # # print(fn())
    # def gn():
    #     i=1
    #     while i<146:
    #         yield "英雄联盟第%s位英雄"%i
    #         i=i+1
    # g1=gn()
    # print(g1.__next__())
    # print(g1.__next__())
    # print(g1.__next__())
    # lst=[]
    # for i in range(1,101):
    #     lst.append(i)
    # print(lst)
    # lst=["python%s"%i for i in range(1,101)]
    # print(lst)
    # print(["python%s"%i for i in range(1,101)])
    # print(["python%s"%i for i in range(1,101)])
    
    # def fn():
    #     print(111)
    #     yield 222
    # g=fn()
    # g1=(i for i in g)
    # g2=(j for j in g1)
    # print(list(g))
    # print(list(g1))
    # print(list(g2))
    #生成器函数
    #yield与return的区别,return是返回结果停止调用;yield也是返回结果但是是分段调用
    # def fn():
    #     print(111)
    #     yield 222
    #     print(333)
    #     a=yield 444
    #     print(a)
    #     print(555)
    #     yield 666
    # g=fn()
    # print(list(g))
    # print(g.__next__())
    # print(g.__next__())
    # print(g.send(11))
    #__next__()与send的区别
    # 都是传给下一个值,但是send不能用于第一个,它可以给yield赋一个值,最后一个也不能传,会报错
    
    # 列表推导式:
    # 列表推倒式; 最终给你的是列表
    # 语法 [最终结果(变量) for 变量 in 可迭代对象]
    # lst=[i for i in range(1,101)]
    # print(lst)
    #1-100能被3整除的数
    # print([i for i in range (1,101) if i%3==0])
    # #100以内能被3整除的平方
    # print([i**2 for i in range (1,101) if i%3==0])
    # names = [['Tom', 'Billy', 'Jefferson' , 'Andrew' , 'Wesley' , 'Steven' ,
    # 'Joe'],'Alice',['Alice', 'Jill' , 'Ana', 'Wendy', 'Jennifer', 'Sherry' , 'Eva']]
    # print([name for i in names for name in i if name.count('e')>=2])
    
    # 生成器表达式:
    # g=(i for i in range(1,101))
    # print(list(g))
    # 生成器的惰性机制:
    # def func():
    #     print(111)
    #     yield  222
    # g = func()
    # g1 = (i  for i in  g)
    # g2 = (i  for i in  g1)
    #
    # print(list(g))#111  [222]
    # print(list(g1))#[]生成器的惰性机制,若不调用不执行
    # print(list(g2))#[]
    
    # 字典和集合的推导式:
    # dic={'a':'b','c':'d'}
    # # dic1={dic[key]:key for key in dic}
    # # print(dic1)
    lst=["盖伦","猴子","宝石"]
    lst1=["卡特","狐狸","ez"]
    print({lst[i]:lst1[i] for i in range (len(lst))})
    # 关于生成器的一个面试题:
    # def add(a, b):
    #     return a + b
    # def gen():
    #     for r_i in range(4):
    #          yield r_i
    # g = gen()
    #
    # for n in [2, 10]:#n存在两种取值,2或者10
    #     g = (add(n, i) for i in g)      #循环两次后n=10,g=(add(n, i) for i in (add(n, i) for i in g) )
    # print(list(g))          #n的取值为最后一次循环的值,代入得20,21,22,23
    
  • 相关阅读:
    MyEclipse添加SVN插件
    Postgresql的character varying = bytea问题
    Hibernate主键增加方式
    java配置环境变量
    Maven常用构建命令
    Postgresql的主键自增长
    js判断金额
    最精简的SQL教程
    SQL练习1:统计班级男女生人数
    sql 百万级数据库优化方案
  • 原文地址:https://www.cnblogs.com/zhangdaye/p/9329672.html
Copyright © 2011-2022 走看看