码迷,mamicode.com
首页 > 其他好文 > 详细

第十二天

时间:2020-02-26 21:05:42      阅读:56      评论:0      收藏:0      [点我收藏+]

标签:生成器   code   了解   代码   注意   转化   生成   name   iter   

生成器

是我们自己用python代码够贱的数据结构.迭代器都是提供的,或者转化得来的.


获取生成器的方式

  • 生成器函数
  • 生成器表达式
  • python内部提供的一些

生成器函数

一个 next 对应 next

#1

def func():
    print(111)
    print(222)
    yield 3
ret=func()
print(ret)
#//执行之后没有执行    函数名+()   函数不执行

print(next(ret))   #这样执行    111  222  3
 

#2

def func():
    print(111)
    print(222)
    yield 3
    yield 4
ret=func()
print(next(ret))      # 111  222 3    一个next对应一个 next

#3
def func():
    print(111)
    print(222)
    yield 3
    yield 4
ret=func()
print(next(ret)) 
print(next(ret))     # 111 222 3  4

return yield 对比

return: 函数中只存在一个return 结束函数并且给函数执行者返回值

yeild:只要函数中有yield name它就是生成器函数而不是函数了

生成器函数中可以存在多个yield ,yield不会结束生成器函数一个,yield对应一个next

例子:吃包子

def func():
    l1=[]
    for i in range(1,5001):
        l1.append(f'{i}号包子')


    return l1
ret=func()
print(ret)

def func():
    l1 = []
    for i in range(1, 5001):
        yield f'{i}号包子'
ret = func()

#吃10个包子
for i in range(1,11):
    print(next(ret))

for i in range(1, 11):
    print(next(ret))
for i in range(1, 11):
    print(next(ret))
for i in range(1, 11):
    print(next(ret))
for i in range(1, 11):
    print(next(ret))

yield from

将可迭代对象变成迭代器返回

def func():
    l1=[1,2,3,4,5]
    yield from l1   #将  L1这个列表变成 了迭代器返回
ret=func()
print(next(ret))     
print(next(ret))    #1   2

列表推导式 生成器表达式

用一行代码构建一个比较复杂有规律的列表

l1=[i for i in range(1,11)]

print(l1)

#//[1,2,3,4,5,6,7,8,9,10]

分为两类

1循环模式:[变量(加工后的变量) for 变量 in iterable ]

l1=[i**2 for i in range(1,11)]
print(l1)


l1=[i for i in range(1,101) if i%2==0 ]
print(l1)

l1=[(f'python{i}期')for i in range(1,101)  ]
print(l1)


#[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]
# ['python1期', 'python2期', 'python3期', 'python4期', 'python5期', 'python6期', 'python7期', 'python8期', 'python9期', 'python10期', 'python11期', 'python12期', 'python13期', 'python14期', 'python15期', 'python16期', 'python17期', 'python18期', 'python19期', 'python20期', 'python21期', 'python22期', 'python23期', 'python24期', 'python25期', 'python26期', 'python27期', 'python28期', 'python29期', 'python30期', 'python31期', 'python32期', 'python33期', 'python34期', 'python35期', 'python36期', 'python37期', 'python38期', 'python39期', 'python40期', 'python41期', 'python42期', 'python43期', 'python44期', 'python45期', 'python46期', 'python47期', 'python48期', 'python49期', 'python50期', 'python51期', 'python52期', 'python53期', 'python54期', 'python55期', 'python56期', 'python57期', 'python58期', 'python59期', 'python60期', 'python61期', 'python62期', 'python63期', 'python64期', 'python65期', 'python66期', 'python67期', 'python68期', 'python69期', 'python70期', 'python71期', 'python72期', 'python73期', 'python74期', 'python75期', 'python76期', 'python77期', 'python78期', 'python79期', 'python80期', 'python81期', 'python82期', 'python83期', 'python84期', 'python85期', 'python86期', 'python87期', 'python88期', 'python89期', 'python90期', 'python91期', 'python92期', 'python93期', 'python94期', 'python95期', 'python96期', 'python97期', 'python98期', 'python99期', 'python100期']

2筛选模式:[变量(加工后的变量) for 变量 in iterable if 条件 ]

将列表中两个e的打印出来 
[name for i in names for name in i if name.count("e")==2]

生成器表达式 与列表的推导式写法几乎一模一样

print((i for i in range (1,11)))  #生成器
print([i for i in range (1,11)])   #列表推导式
a=(i for i in range (1,11))

print(next(a))
print(next(a))
print(next(a))
print(next(a))
print(next(a))
print(next(a))

# 也可以
for i in a :
    print(i)

总结

列表推导式

缺点:

  1. 智能构建比较复杂的并且有规律的列表 不要着迷
  2. 超过三层才能构建成功的 不建议使用
  3. 查找错误(debug)不行

优点:

  1. 一行构建,简单
  2. 装逼

构建一个列表[1,2,3,4,5,6,"j","Q","K"]

l1=[i for i in range(1,7)]+list("JQK")

列表推导式 于生成器表达式区别

写法上:[] ()

iterable iterator

了解

字典推导式

lst1=["age"]

lst2=["18"]

dic={lst1[i]:lst2[i] for i in range(len(lst1))}

集合推导式

{}

{for i in range(1,11)} #//出来是一个集合

内置函数

python提供了68个内置函数

eval 剥去字符串的外衣 运算里面的代码

注意: 网络传输的时候 str input 输入的时候. sql注入等等绝对不能使用 eval

s1=‘1+2‘

print(eval(s1)) #3

s1=‘{"name":"小屋"}‘

他是字符串 怎么操作字典呢? ↓↓↓↓↓

eval(s1)

exec 与 eval 几乎一样 代码流(尽量不用)


hash 操作不可变的数据类型

得到哈希值

help

print(help(str.upper))

callable 判断一个对象是否可以调用

第十二天

标签:生成器   code   了解   代码   注意   转化   生成   name   iter   

原文地址:https://www.cnblogs.com/xueba/p/12368683.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!