fs.append(f)这是在list里增加一个函数 而 fs.append(f())则变成了在list里增加一个实例
2016-08-02
def count():
fs = []
for i in range(1, 4):
def f(x):
def g():
return x*x
return g
r = f(i)
fs.append(r)
return fs
fs = []
for i in range(1, 4):
def f(x):
def g():
return x*x
return g
r = f(i)
fs.append(r)
return fs
2016-08-02
def calc_prod(lst):
def prod():
return reduce(lambda x,y:x*y,lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod():
return reduce(lambda x,y:x*y,lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-08-01
def cmp_ignore_case(s1, s2):
if s1.lower() < s2.lower():
return -1
if s1.lower() > s2.lower():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.lower() < s2.lower():
return -1
if s1.lower() > s2.lower():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-07-31
一、
import math
def is_sqr(x):
return x in [i**2 for i in range(1,11)]
print filter(is_sqr, range(1, 101))
二、def is_sqr(x):
return [x for i in range(1,11) if x==i**2]
print filter(is_sqr, range(1, 101))
import math
def is_sqr(x):
return x in [i**2 for i in range(1,11)]
print filter(is_sqr, range(1, 101))
二、def is_sqr(x):
return [x for i in range(1,11) if x==i**2]
print filter(is_sqr, range(1, 101))
2016-07-31
def format_name(s):
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
2016-07-31
import math
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt)
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt)
2016-07-31