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
import math
def gen(x):
y=0.0
while(1):
y=y+1
if y*y==x:
break
return y
def add(x, y, f):
return f(x) + f(y)
print add(25,9, gen)
def gen(x):
y=0.0
while(1):
y=y+1
if y*y==x:
break
return y
def add(x, y, f):
return f(x) + f(y)
print add(25,9, gen)
2018-05-10
def format_name(s):
i=s[0].upper()
i=i+s[1:].lower()
return i
print map(format_name, ['adam', 'LISA', 'barT'])
i=s[0].upper()
i=i+s[1:].lower()
return i
print map(format_name, ['adam', 'LISA', 'barT'])
2018-05-10
最赞回答 / 慕少9221509
在Python 3里,reduce() 函数已经被从全局名字空间里移除了,它现在被放置在fucntools 模块里使用前需要先引用: from functools import reduce
2018-05-09
def __cmp__(self, s):
if self.score>s.score:
return -1
elif self.score<s.score:
return 1
else:
if self.name<s.name:
return -1
elif self.name>s.name:
return 1
else:
return 0
if self.score>s.score:
return -1
elif self.score<s.score:
return 1
else:
if self.name<s.name:
return -1
elif self.name>s.name:
return 1
else:
return 0
2018-05-09
f1, f2, f3 = count() count()的返回值是一个list的,所以f1,f2,f3也应该是list,f1,f2,f3是list,里面的元素是指向函数的变量,printf f1( ),f2( ),f3( ) 这条语句是不是可以这么理解,打印f1中元素再加上( )就是调用l函数
2018-05-09
def f1(x,y):
return x*y
def calc_prod(lst):
def cal():
return reduce(f1,lst)
return cal
f = calc_prod([1, 2, 3, 4])
print f()
return x*y
def calc_prod(lst):
def cal():
return reduce(f1,lst)
return cal
f = calc_prod([1, 2, 3, 4])
print f()
2018-05-09
def hanshu1(list):
def hanshu2(x,y):
return x*y
return reduce(hanshu2, list)
print hanshu1(range(1,5))
def hanshu2(x,y):
return x*y
return reduce(hanshu2, list)
print hanshu1(range(1,5))
2018-05-08
def f(s):
return s[0].upper()+s[1:].lower()
print map(f, ['adam', 'LISA', 'barT'])
return s[0].upper()+s[1:].lower()
print map(f, ['adam', 'LISA', 'barT'])
2018-05-08