day14............生成器, 三元,推导

1  生成器

'''

1 什么是生成器?
在函数内但凡出现yield关键字,再调用函数就不会执行函数体代码,会返回值一个值,该值称之为生成器
 生成器本质就是迭代器

2、为什么要有生成器?
    生成器是一种自定义迭代器的方式

3、如何用生成器


'''



# def func():
#     print('first1')
#     print('first2')
#     print('first3')
#     yield 1 #暂停
#     print('second1')
#     print('second2')
#     print('second3')
#     yield 2  #暂停
#     print('third')
#     yield 3 #暂停
#     print('fourth')
#
# g=func()
# print(g)
# print(g.__iter__().__iter__().__iter__() is g)
# res1=next(g)
# print('第一次的返回值:',res1)
#
# print('='*100)
# res2=next(g)
# print('第二次的返回值:',res2)
#
# print('='*100)
# res3=next(g)
# print('第三次的返回值:',res3)
#
# print('='*100)
# res4=next(g)
# print('第三次的返回值:',res4)

# for item in g: #g=iter(g) #item=next(g)
#     print(item)

# i=range(1,1000)
# for item in range(1,10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000):
#     print(item)



# def my_range(start,stop,step=1):
#     while start < stop:
#         yield start # 暂停
#         start+=step
# g=my_range(1,5,2) #1 3


# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))

# for item in g:
#     print(item)


#总结yield的功能
#1、提供一种自定义迭代器的方式
#2、yield可以暂停住函数,返回值

#yield  VS return
#相同点:都是用在函数内,都可以返回值,没有类型限制,没有个数限制
#不同点:return只能返回一次值,yield可以返回多次值


# 了解知识
# yield 值
# x=yield
# x= yield 值
View Code

2 三元,推导

#三元表达式
# 条件成立时的返回值 if 条件 else 条件不成立时的返回值

# def max2(x,y):
#     if x > y:
#         return x
#     else:
#         return y
# x=10
# y=20
# res=x if x > y else y
# print(res)

# 列表生成式
# l=[item**2 for item in range(1,11)]
# print(l)


# names=['alex','wxx','lxx']

# l=[]
# for name in names:
#     l.append(name + 'SB')
# names=l

# names=[name+'SB' for name in names]
# print(names)


# names=['alex','wxx','egon','lxx','zhangmingyan']
# l=[]
# for name in names:
#     if name != 'egon':
#         l.append(name + 'SB')
# names=l
# names=[name+'SB' for name in names if name != 'egon']
# print(names)


# l=[item**2 for item in range(1,5) if item > 2]
# print(l)

# names=['egon','alex_sb','wupeiqi','yuanhao']
# names=[name.upper() for name in names]
# print(names)
# names=['egon','alex_sb','wupeiqi','yuanhao']
#
# nums=[len(name) for name in names if not name.endswith('sb')]
# print(nums)


#字典生成式
# s1='hello'
# l1=[1,2,3,4,5]

# res=zip(s1,l1)
# print(res)
# print(list(res))


# keys=['name','age','sex']
# values=['egon',18,'male']

# res=zip(keys,values)
# print(list(res))
# print(list(res))
# d={}
# for k,v in zip(keys,values):
#     d[k]=v
# print(d)

# keys=['name','age','sex']
# values=['egon',18,'male']
# d={k:v for k,v in zip(keys,values)}
# print(d)

info={'name': 'egon', 'age': 18, 'sex': 'male'}

keys=info.keys()
# print(keys)
# iter_keys=keys.__iter__()
# values=info.values()
# print(values)

# d={k:v for k,v in zip(keys,values)}
# print(d)

# s={i for i in range(10)}
# print(s,type(s))



# 生成器表达式
# g=(i for i in range(10))
# # print(g)
#
# print(next(g))
# print(next(g))


# nums=[11,22,33,44,55]
# print(max(nums))

# with open('a.txt',encoding='utf-8') as f:
#     nums=(len(line) for line in f)
#     print(max(nums))
# print(max(nums))
# print(max(nums))



l=['egg%s' %i for i in range(100)]
print(l)

g=('egg%s' %i for i in range(1000000000000))
# print(g)
print(next(g))
print(next(g))
View Code

 

posted @ 2018-06-13 16:51  谢金涛  阅读(94)  评论(0编辑  收藏  举报