def calc_prod(lst):
def lazy_calc():
return reduce(lambda x,y:x*y,lst)
return lazy_calc
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_calc():
return reduce(lambda x,y:x*y,lst)
return lazy_calc
f = calc_prod([1, 2, 3, 4])
print f()
2015-05-19
def calc_prod(lst):
def multipt():
sum=1
for i in lst:
sum=sum*i
return sum
return multipt
def multipt():
sum=1
for i in lst:
sum=sum*i
return sum
return multipt
2015-05-18
def is_not_empty(s):
return s and len(s.strip()) > 0
print filter(lambda s:s and s==s.strip(), ['test', None, '', 'str', ' ', 'END'])
return s and len(s.strip()) > 0
print filter(lambda s:s and s==s.strip(), ['test', None, '', 'str', ' ', 'END'])
2015-05-18
def calc_prod(lst):
def lazy_prod():
return reduce(lambda x,y:x*y ,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_prod():
return reduce(lambda x,y:x*y ,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2015-05-18
def count():
fs = []
for i in range(1, 4):
g = lambda x:lambda :x*x
fs.append(g(i))
return fs
啊写完感觉好晕
fs = []
for i in range(1, 4):
g = lambda x:lambda :x*x
fs.append(g(i))
return fs
啊写完感觉好晕
2015-05-18
它可以正确地返回一个闭包g,g所引用的变量j不是循环变量,因此将正常执行。
在count函数的循环内部,如果借助f函数,就可以避免引用循环变量i。
在count函数的循环内部,如果借助f函数,就可以避免引用循环变量i。
2015-05-18
最后的思考这样吗:
def is_Student(x):
return isinstance(x, Student)
L = [Student('Tim', 99), Student('Bob', 88), 100, 'Hello']
print sorted(filter(is_Student, L))
def is_Student(x):
return isinstance(x, Student)
L = [Student('Tim', 99), Student('Bob', 88), 100, 'Hello']
print sorted(filter(is_Student, L))
2015-05-17
a = [x * x for x in range(1, 11)]
def is_sqr(x):
return a.count(x)
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return a.count(x)
print filter(is_sqr, range(1, 101))
2015-05-17
调用calc_sum()并没有计算出结果,而是返回函数:
>>> f = calc_sum([1, 2, 3, 4])
>>> f
<function lazy_sum at 0x1037bfaa0>
# 对返回的函数进行调用时,才计算出结果:
>>> f()
10
>>> f = calc_sum([1, 2, 3, 4])
>>> f
<function lazy_sum at 0x1037bfaa0>
# 对返回的函数进行调用时,才计算出结果:
>>> f()
10
2015-05-16
sorted_ignore_case = functools.partial(sorted, key=lambda x:x.lower())
2015-05-15
def f(x):
return x[0:1].capitalize()+x[1:].lower()
print map(f,['adam', 'LISA', 'barT'])
return x[0:1].capitalize()+x[1:].lower()
print map(f,['adam', 'LISA', 'barT'])
2015-05-15