def kc(x, y):
return x*y
def calc_prod(lst):
def cacut():
return reduce(kc, lst)
return cacut
f = calc_prod([1, 2, 3, 4])
print f()
return x*y
def calc_prod(lst):
def cacut():
return reduce(kc, lst)
return cacut
f = calc_prod([1, 2, 3, 4])
print f()
2018-12-26
import math
def is_sqr(x):
return x%math.sqrt(x)==0
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return x%math.sqrt(x)==0
print filter(is_sqr, range(1, 101))
2018-12-26
def format_name(s):
return s[0].upper() + s[1:].lower()
s = ['adam', 'LISA', 'barT']
print (map(format_name, ['adam', 'LISA', 'barT']))
return s[0].upper() + s[1:].lower()
s = ['adam', 'LISA', 'barT']
print (map(format_name, ['adam', 'LISA', 'barT']))
2018-12-26
def cmp_ignore_case(s1, s2):
if s1.lower() > s2.lower():
return 1
if s1.lower() < s2.lower():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.lower() > s2.lower():
return 1
if s1.lower() < s2.lower():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-12-20
import math
def is_sqr(x):
return x % (math.sqrt(x)) == 0
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return x % (math.sqrt(x)) == 0
print filter(is_sqr, range(1, 101))
2018-12-20
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()
终于懂了,不知道理解的对不对
首先计算x与y的乘积,乘积返回给上层函数lazy_prod(),
lazy_prod()这个函数中 reduce返回f函数,和你需要传值的列表的参数lst,1可要可不要,这个结果return给上层函数,得出结果。
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()
终于懂了,不知道理解的对不对
首先计算x与y的乘积,乘积返回给上层函数lazy_prod(),
lazy_prod()这个函数中 reduce返回f函数,和你需要传值的列表的参数lst,1可要可不要,这个结果return给上层函数,得出结果。
2018-12-18
import functools
sorted_ignore_case = functools.partial(sorted,key=str.lower)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
sorted_ignore_case = functools.partial(sorted,key=str.lower)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
2018-12-18
class Person(object):
__count = 0
def __init__(self, name):
Person.__count += 1
self.name = name
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except:
print 'AttributeError'
__count = 0
def __init__(self, name):
Person.__count += 1
self.name = name
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except:
print 'AttributeError'
2018-12-17
import math
def is_sqr(x):
return int(math.sqrt(x)) ** 2 == x
print filter(is_sqr, range(1, 101))
math.sqrt(x)返回的都是浮点类型
def is_sqr(x):
return int(math.sqrt(x)) ** 2 == x
print filter(is_sqr, range(1, 101))
math.sqrt(x)返回的都是浮点类型
2018-12-16
import time
import functools
def performance(f):
def newf(*args,**kw):
time.clock()
f(*args,**kw)
print('函数%s的运行时间为%f秒。'%(f.__name__,time.clock()))
return f(*args,**kw)
return newf
@performance
def factorial(n):
return functools.reduce(lambda x,y: x*y,
import functools
def performance(f):
def newf(*args,**kw):
time.clock()
f(*args,**kw)
print('函数%s的运行时间为%f秒。'%(f.__name__,time.clock()))
return f(*args,**kw)
return newf
@performance
def factorial(n):
return functools.reduce(lambda x,y: x*y,
2018-12-12