print filter(lambda s: s and len(s.strip())>0, ['test', None, '', 'str', ' ', 'END'])
2016-03-02
def count():
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
return g
r=f(i)
fs.append(r)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
return g
r=f(i)
fs.append(r)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2016-03-02
def calc_prod(lst):
def lazy_calc_prod():
s=1
for x in lst:
s=s*x
return s
return lazy_calc_prod
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_calc_prod():
s=1
for x in lst:
s=s*x
return s
return lazy_calc_prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-03-01
def cmp_ignore_case(s1, s2):
t1=s1[0].upper()
t2=s2[0].upper()
if t1>t2:
return 1
if t1<t2:
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
t1=s1[0].upper()
t2=s2[0].upper()
if t1>t2:
return 1
if t1<t2:
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-03-01
import math
def is_sqr(x):
return math.sqrt(x)==int(math.sqrt(x))
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return math.sqrt(x)==int(math.sqrt(x))
print filter(is_sqr, range(1, 101))
2016-03-01
def calc_prod(lst):
def lazy_calc(lst):
reduce(lambda x,y:x*y,lst,1)
return lazy_calc
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_calc(lst):
reduce(lambda x,y:x*y,lst,1)
return lazy_calc
f = calc_prod([1, 2, 3, 4])
print f()
2016-03-01
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-03-01
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-03-01
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-03-01
已采纳回答 / AwesomeOcean
*args表示任何多个无名参数,它是一个tuple;**kwargs表示关键字参数,它是一个dict。并且同时使用*args和**kwargs时,必须*args参数列要在**kwargs前.例子:def test(a,*args,**kwargs): print a #print b #print c print args print kwargstest(1,2,3,d='4',e=5)输出结果:1(2, 3){'e': 5, 'd': '4'}意思就是1还是参数a的值,a...
2016-03-01
import math
def is_sqr(x):
return math.sqrt(x)==int(math.sqrt(x))
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return math.sqrt(x)==int(math.sqrt(x))
print filter(is_sqr, range(1, 101))
2016-03-01
最赞回答 / 再见你
题主是说的答案里的打印问题是吗?其实是这样的,要先了解一下问题,Person这个类中__init__这个函数是什么意思。<...code...>这句话就意味着,p1进行了初始化,也就是说,调用了Person.__init_...
2016-02-29