在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
已采纳回答 / 微笑的芒果
sorted调用匿名函数对L1进行排序操作;分步解释如下:1.匿名函数调用cmp返回p1.name和p2.name的比较,排序按ASCII码进行从小到大比较2.cmp函数:比较两个参数,参数1>参数2:1,参数1<参数2:-1,参数1=参数2:0
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
最赞回答 / 小钱趣
完整点分析这个答案或许更好理解1、functools.partial的用法为:functools.partial(模板,默认参数),这里用的是stored的函数模板2、可以在python内用help来查看stored的参数定义,如下: sorted(...) sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list3、用cmp参数比较大小,由于要调用cmp函数,为了代码的简化使用匿名函数lamb...
2016-04-05
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