python 进阶
今天看到一个用法很好,把页面上的cookies 转成dict cookies = dict(line.split('=') for line in cookie.strip().split(';'))
def generator_function(): for i in range(10): yield i if __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 squared2 def multiply(x): return x*x def add(x): return x+x funcs = [multiply,add] for i in items: value = map(lambda x: x(i),funcs)#x代表funcs中的对象(它的对象是两个函数),i是x的参数 print value