reduce()函数也是Python内置的一个高阶函数。reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接收两个参数,reduce()对list的每个元素反复调用函数f,并返回最终结果值。
2017-09-13
说得太玄了?原代码返回的函数用的变量是FOR里的变量,运行函数时找变量值找到已经跑完了的计数器。新代码里面就是函数里的变量在FOR运行的时候先把每次的值记下来,运行时函数里已经是确定的值。
2017-09-13
print(list(map(lambda s:s and len(s.strip())>0,['test', ' ', None, 'str', 'End'])))
只能输出true与false。。。。。。。。。。。。
只能输出true与false。。。。。。。。。。。。
2017-09-12
试了各种方法,只要不是写成fs.append(f()),就出现:
<function count.<locals>.f at 0x0000000000DCC510> <function count.<locals>.f at 0x0000000000DCC598> <function count.<locals>.f at 0x0000000000DCC620>
<function count.<locals>.f at 0x0000000000DCC510> <function count.<locals>.f at 0x0000000000DCC598> <function count.<locals>.f at 0x0000000000DCC620>
2017-09-12
def format_name(s):
s=s.lower()
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
s=s.lower()
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
2017-09-12
import math
def is_sqr(x):
return math.sqrt(x) % 1 == 0
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return math.sqrt(x) % 1 == 0
print filter(is_sqr, range(1, 101))
2017-09-12
class Fib(object):
def __init__(self, num=1):
a, b, self.L = 0, 1, []
for n in range(0,num):
self.L.append(a)
t = b
b = a + b
a = t
def __call__(self, num):
self.__init__(num)
return self.L
f = Fib()
print f(10)
def __init__(self, num=1):
a, b, self.L = 0, 1, []
for n in range(0,num):
self.L.append(a)
t = b
b = a + b
a = t
def __call__(self, num):
self.__init__(num)
return self.L
f = Fib()
print f(10)
2017-09-11
def calc_prod(lst):
def lazy_prod():
sum=1
for x in lst:
sum*=x
return sum
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_prod():
sum=1
for x in lst:
sum*=x
return sum
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2017-09-11
print(sorted(['bob','about','Zoo','Credit'],key=str.lower))
2017-09-11
import time
def performance(f):
def ptime(*args,**kw):
printf time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
return f
return ptime
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
系统繁忙,先放这儿
def performance(f):
def ptime(*args,**kw):
printf time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
return f
return ptime
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
系统繁忙,先放这儿
2017-09-11