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
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
方法一:
import math
def is_sqr(x):
return math.sqrt(x) == int(math.sqrt(x))
print filter(is_sqr, range(1, 101))
方法二:
import math
def is_sqr(x):
return math.sqrt(x) % 1 == 0
print filter(is_sqr, range(1, 101))
import math
def is_sqr(x):
return math.sqrt(x) == int(math.sqrt(x))
print filter(is_sqr, range(1, 101))
方法二:
import math
def is_sqr(x):
return math.sqrt(x) % 1 == 0
print filter(is_sqr, range(1, 101))
2018-05-08
def calc_prod(lst):
def calc_a():
def a(x,y):
return x*y
return reduce(a,lst)
return calc_a
f = calc_prod([1, 2, 3, 4])
print f()
def calc_a():
def a(x,y):
return x*y
return reduce(a,lst)
return calc_a
f = calc_prod([1, 2, 3, 4])
print f()
2018-05-08
def cmp_ignore_case(s1, s2):
if s1.lower()>s2.lower() :
return 1
if s1.lower()<s2.lower() :
return -1
return
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.lower()>s2.lower() :
return 1
if s1.lower()<s2.lower() :
return -1
return
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-05-08
import math
def is_sqr(x):
if math.sqrt(x)==int(math.sqrt(x)):
return x*x
print filter(is_sqr, range(1,101))
def is_sqr(x):
if math.sqrt(x)==int(math.sqrt(x)):
return x*x
print filter(is_sqr, range(1,101))
2018-05-08
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])
2018-05-08
方法一:
def prod(x, y):
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
def prod(x, y):
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
2018-05-08
方法二:
l_o = ['adman', 'LISA', 'batT']
def format(s):
l_n = s[0].upper()
for i in s[1:]:
l_n = l_n + i.lower()
return l_n
print map(format, l_o)
l_o = ['adman', 'LISA', 'batT']
def format(s):
l_n = s[0].upper()
for i in s[1:]:
l_n = l_n + i.lower()
return l_n
print map(format, l_o)
2018-05-08