任务是写法有点Low了,py自带这个函数的:
def format_name(s):
return str.capitalize(s)
print map(format_name, ['adam', 'LISA', 'barT'])
def format_name(s):
return str.capitalize(s)
print map(format_name, ['adam', 'LISA', 'barT'])
2016-03-25
这样应该也可以:
def is_sqr(x):
r = int(math.sqrt(x))
s = r * r
if s == x:
return s
def is_sqr(x):
r = int(math.sqrt(x))
s = r * r
if s == x:
return s
2016-03-25
import time
def performance(f):
def re(n):
start = time.time()
data = f(n)
end = time.time()
print "call factorial() in:" + str(end - start)
return data
return re
---out put--完美--
call factorial() in:0.00667309761047
3628800
def performance(f):
def re(n):
start = time.time()
data = f(n)
end = time.time()
print "call factorial() in:" + str(end - start)
return data
return re
---out put--完美--
call factorial() in:0.00667309761047
3628800
2016-03-24
在python3 中,filter跟map类似,结果得用list封装一下。如作业题中,代码修改如下:
import math
def is_sqr(x):
r = int(math.sqrt(x))
return r * r == x
print (list(filter(is_sqr, range(1, 101))))
import math
def is_sqr(x):
r = int(math.sqrt(x))
return r * r == x
print (list(filter(is_sqr, range(1, 101))))
2016-03-23
python3中,要使用reduce,得从functools中引入,加上:
from functools import reduce
才能够用。
from functools import reduce
才能够用。
2016-03-23
def cmp_ignore_case(s1, s2):
if s1[0].lower()<s2[0].lower():
return -1
if s1[0].lower()>s2[0].lower():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'],cmp_ignore_case)
if s1[0].lower()<s2[0].lower():
return -1
if s1[0].lower()>s2[0].lower():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'],cmp_ignore_case)
2016-03-23
def calc_prod(lst):
def lazy_prod():
return reduce(lambda x,y: x*y, lst)
return lazy_prod
def lazy_prod():
return reduce(lambda x,y: x*y, lst)
return lazy_prod
2016-03-22