def calc_prod(lst):
def prod(x, y):
return x*y
def calc():
return reduce(prod, lst)
return calc
f = calc_prod([1, 2, 3, 4])
print f()
def prod(x, y):
return x*y
def calc():
return reduce(prod, lst)
return calc
f = calc_prod([1, 2, 3, 4])
print f()
2018-04-18
from functools import reduce
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-04-18
已采纳回答 / sakuart
python3 sorted取消了对cmp的支持。sorted(iterable, key=None, reverse=False)reverse是一个布尔值。如果设置为True,列表元素将被倒序排列,默认为Falsekey接受一个函数,这个函数只接受一个元素,默认为Nonesorted([36, 5, 12, 9, 21], reverse=True)就可以实现倒序Python2中的自定义布尔函数cmp=custom_cmp(x, y)由Python3中的key=custom_key(x)代替。在pyt...
2018-04-17
阻挡我们学习代码的不是思维而是翻译,什么叫闭包,我的感觉叫“越权行为”都更加直观(翻译不好请多指正),匿名就更离谱了,是我们熟悉的匿名的意思吗,叫“裸体”函数都直观一些
2018-04-17
def cmp_ignore_case(s1, s2):
return ((s1.lower() > s2.lower()) - (s1.lower() < s2.lower()))
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
return ((s1.lower() > s2.lower()) - (s1.lower() < s2.lower()))
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-04-17
class BStudent(Student,BasketballMixin):
def __init__(self):
Student.__init__(self)
BasketballMixin.__init__(self)
class FTeacher(Teacher,FootballMixin):
def __init__(self):
Teacher.__init__(self)
FootballMixin.__init__(self)
这样就行啦,先去看他的实例,怎么能让实例的方法执行
def __init__(self):
Student.__init__(self)
BasketballMixin.__init__(self)
class FTeacher(Teacher,FootballMixin):
def __init__(self):
Teacher.__init__(self)
FootballMixin.__init__(self)
这样就行啦,先去看他的实例,怎么能让实例的方法执行
2018-04-14
def cmp_ignore_case(s1, s2):
if s1.upper()<s2.upper():
return -1
elif s1.upper()==s2.upper():
return 0
else:
return 1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.upper()<s2.upper():
return -1
elif s1.upper()==s2.upper():
return 0
else:
return 1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-04-13