# 生成器函数 yield
def func():
print(1)
print(2)
yield 3
a = 1
b = 2
c = a+b
yield 4
ret = func()
print(next(ret))
...
# 一个next对应一个yield
# yield from
def func():
l1 = [1,2,3,4,5]
l2 = ['a','b','c']
yield from l1
yield from l2
ret = func()
for i in ret:
print(i) # 1,2,3,4,5,a,b,c 不会进行交替输出
# 将l1列表变成迭代器返回
# 从python1期到python100期写入列表lst
lst = [f'python{i}' % i for i in range(1,101)]
# 三十以内可以被三整除的数
lst = [i for i in range(30) if i % 3 is 0]
# 找到嵌套列表中名字含有两个‘e’的所有名字
names = [['Tom', 'Billy', 'Jefferson', 'Andrew', 'Wesley', 'Steven', 'Joe'],
['Alice', 'Jill', 'Ana', 'Wendy', 'Jennifer', 'Sherry', 'Eva']]
lst = [name for lst in names for name in lst if name.count('e') >= 2]
# 拆分写法
lst = []
for lst in names:
for name in lst:
if name.count('e')>=2
lst.append(name)
同列表推导式只是将 [] 换成 () 生成的是一个生成器
# 根据名字应该也能猜到,推到出来的是字典
lst1 = ['jay','jj','meet']
lst2 = ['周杰伦','林俊杰','郭宝元']
dic = {lst1[i]:lst2[i] for i in range(len(lst1))}
print(dic)
lst = [1,2,3,-1,-3,-7,9]
s = {abs(i) for i in lst}
print(s)