def calc_prod(lst):
def lazy_prod():
def f(x,y):
return x * y
return reduce(f, lst, 1)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_prod():
def f(x,y):
return x * y
return reduce(f, lst, 1)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-02-21
def cmp_ignore_case(s1, s2):
s1 = s1.upper()
s2 = s2.upper()
if s1 > s2:
return 1
if s1 < s2:
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
s1 = s1.upper()
s2 = s2.upper()
if s1 > s2:
return 1
if s1 < s2:
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-02-20
import math
def is_sqr(x):
r = math.sqrt(x)
return r%1==0
print filter(is_sqr, range(1, 101))
def is_sqr(x):
r = math.sqrt(x)
return r%1==0
print filter(is_sqr, range(1, 101))
2016-02-20
def prod(x, y):
return x * y
print reduce(prod, [2, 4, 5, 7, 12])
return x * y
print reduce(prod, [2, 4, 5, 7, 12])
2016-02-20
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'])
2016-02-20
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-02-20
def calc_prod(lst):
def c():
j=1
for i in lst:
j*=i
print j
return c
f = calc_prod([1, 2, 3, 4])
print f()
def c():
j=1
for i in lst:
j*=i
print j
return c
f = calc_prod([1, 2, 3, 4])
print f()
2016-02-20
def format_name(s):
return s[0].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
def format_name(s):
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[0].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
def format_name(s):
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
2016-02-20
def calc_prod(lst):
def my_prod(a,b):
return a*b
def lazy_prod():
return reduce(my_prod,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def my_prod(a,b):
return a*b
def lazy_prod():
return reduce(my_prod,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-02-19
最新回答 / 飞雪随风
这个是Python参考文档的一部分sorted(iterable[,
cmp[, key[, reverse]]])Return a new sorted list from the items in iterable.The optional arguments cmp, key, and reverse have
the same meaning as those for the list.sort() method (described in section Mutable
Sequence ...
2016-02-19