python:practice built-in function

built-in function,is python already prepar for us ,anytime we can call built-in function when we needed it .

all()  dict()   help()

all([1,2,'')

eval('1+2+3')

filter(function,sequence)

filter object  filter iterator

map(function,sequence)

map object map iterator

map 

can realize iterator function

for example:

str=['a','b','c','d']

def fun(str):

   if  str !='a':

       return str

ret =filter(fun,str)

print(list(ret))

str =['a','b','c','d']

def fun2 (str):

   return str+'good'

ret = map(fun2,str)

print(ret)

reduce:

from fuctools import reduce

def add(x+y):

  return x+y

print(reduce(add,range(1,10))

1+2+3+4+5+6+7+8+9=45

the result of reduce is a value ,object not 

lambda:

add=lambda a,b:a+b

conbine reduce and  lambda  realize factorial effect:

from fuctools import reduce 

reduce(lambda a,b:a+b,range(1,101))  5050

reduce(lambda a,b:a*b,range(1,10))

原文地址:https://www.cnblogs.com/alansuny/p/12496517.html