def calc_product(lst):
def lazy_product(x,y):
return x*y
return reduce(lazy_product,lst)
return lazy_product
f=calc_product([1,2,3,4])
print f
def lazy_product(x,y):
return x*y
return reduce(lazy_product,lst)
return lazy_product
f=calc_product([1,2,3,4])
print f
2018-05-14
def format_name(s):
return s[0].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[0].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-05-13
这一节主要讲的是闭包,而闭包就是说返回函数时所取的参数不能是变化的,所以再加一层函数,使返回的函数引用固定值就好了。或者像楼上说的,append(f())就好了,f()就计算了结果,返回的不是函数了。
2018-05-13
看懂这一节需要储备一下补丁参数函数。另外再看一下https://blog.csdn.net/xiangxianghehe/article/details/77170585 这个文章,会更容易懂。
2018-05-13
def calc_prod(lst):
a = 0
c = 1
while a < len(lst):
c = c * lst[a]
a += 1
return c
f = calc_prod([1, 2, 3, 4])
print f
这个多层的我真的不会,转不过来
a = 0
c = 1
while a < len(lst):
c = c * lst[a]
a += 1
return c
f = calc_prod([1, 2, 3, 4])
print f
这个多层的我真的不会,转不过来
2018-05-13
import functools
sorted_ignore_case = functools.partial(sorted,key=lambda x:x.lower())
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
sorted_ignore_case = functools.partial(sorted,key=lambda x:x.lower())
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
2018-05-13
def f_name(s):
s1=s.title()
return s1
print map(f_name,['adam', 'LISA', 'barT' ])
s1=s.title()
return s1
print map(f_name,['adam', 'LISA', 'barT' ])
2018-05-13
def performance(f):
def n(*args,**kwargs):
startime = time.time()
res = f(*args,**kwargs)
print'call',f.__name__+'()','in',time.time()-startime
return res
return n
def n(*args,**kwargs):
startime = time.time()
res = f(*args,**kwargs)
print'call',f.__name__+'()','in',time.time()-startime
return res
return n
2018-05-12
def calc_prod(lst):
def cj():
i=1
for a in lst:
i=i*a
return i
return cj
f = calc_prod([1, 2, 3, 4])
print f()
return i
return he
f = calc_prod([1, 2, 3, 4])
print f()
def cj():
i=1
for a in lst:
i=i*a
return i
return cj
f = calc_prod([1, 2, 3, 4])
print f()
return i
return he
f = calc_prod([1, 2, 3, 4])
print f()
2018-05-11
import math
def add(x, y, f):
return f(x) + f(y)
def gh(z):
return z**0.5
print add(25, 9, gh)
def add(x, y, f):
return f(x) + f(y)
def gh(z):
return z**0.5
print add(25, 9, gh)
2018-05-11
print filter(lambda s:s and len(s.strip())>0,['test', None, '', 'str', ' ', 'END'])
2018-05-10
def count():
fs = []
for i in range(1, 4):
def c(m=i):
return m*m
fs.append(c)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def c(m=i):
return m*m
fs.append(c)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2018-05-10