首页 > 代码库 > python 进阶

python 进阶

def generator_function():	for i in range(10):		yield iif __name__ == ‘__main__‘:	for item in generator_function():		print item	gen = generator_function()	print next(gen)	print next(gen)	print next(gen)	print next(gen)	l = iter(range(5))	print next(l)	my_name = ‘Zsl‘	my_iter = iter(my_name)	print type(my_iter)	print type(next(my_iter))

  生成器可以提升性能

#coding:utf8‘‘‘@map,的用法理解‘‘‘items = range(5)squared = []for i in items:    squared.append(i**3)print squared#对比-------------squared2 = map(lambda x: x**3,items)print squared2def multiply(x):    return x*xdef add(x):    return x+xfuncs = [multiply,add]for i in items:    value = map(lambda x: x(i),funcs)#x代表funcs中的对象(它的对象是两个函数),i是x的参数    print value

 

python 进阶