def calc_prod(lst):
def lazy_func():
sum = 1
for x in lst:
sum *= x
return sum
return lazy_func
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_func():
sum = 1
for x in lst:
sum *= x
return sum
return lazy_func
f = calc_prod([1, 2, 3, 4])
print f()
2016-04-08
在python3的版本中要这样写才行print (list(filter(is_sqr, range(1, 101))))
2016-04-07
Pythonic 很重要但并不是唯一的准则,The Choice Is Yours。
map(function, iterable, ...)/filter(function, iterable)
# map 函数的模拟实现
def myMap(func, iterable):
for arg in iterable:
yield func(arg)
names = ["ana", "bob", "dogge"]
print(map(lambda x: x.capitalize(), names)) # Python 2.7 中直接返回列表
for name
map(function, iterable, ...)/filter(function, iterable)
# map 函数的模拟实现
def myMap(func, iterable):
for arg in iterable:
yield func(arg)
names = ["ana", "bob", "dogge"]
print(map(lambda x: x.capitalize(), names)) # Python 2.7 中直接返回列表
for name
2016-04-07
def count():
fs = []
for i in range(1, 4):
def f(j):
def g():
return j * j
return g
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
返回 1, 4,9
fs = []
for i in range(1, 4):
def f(j):
def g():
return j * j
return g
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
返回 1, 4,9
2016-04-06
r'...' is a byte string (in Python 2.*), ur'...' is a Unicode string (again, in Python 2.*)
2016-04-06
def f(x, y):
return x * y
def calc_prod(lst):
def lazy_prod():
return reduce(f, lst, 1)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
return x * y
def calc_prod(lst):
def lazy_prod():
return reduce(f, lst, 1)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-04-06
def performance(unit):
def a(f):
def b(*args, **kwargs):
print 'call'+f.__name__+'() in %s' %(unit)
return f(*args, **kwargs)
return b
return a
@performance('ms')
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
def a(f):
def b(*args, **kwargs):
print 'call'+f.__name__+'() in %s' %(unit)
return f(*args, **kwargs)
return b
return a
@performance('ms')
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2016-04-05
def calc_prod(lst):
def s(x,y):
return x*y
return reduce(s,lst)
f = calc_prod([1, 2, 3, 4])
print f
def s(x,y):
return x*y
return reduce(s,lst)
f = calc_prod([1, 2, 3, 4])
print f
2016-04-05
def calc_prod(lst):
def lazy_prod():
m=1;
for i in lst:
m*=i;
return m;
return lazy_prod;
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_prod():
m=1;
for i in lst:
m*=i;
return m;
return lazy_prod;
f = calc_prod([1, 2, 3, 4])
print f()
2016-04-04