python:practice built-in function

Posted alansuny

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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))

 

以上是关于python:practice built-in function的主要内容,如果未能解决你的问题,请参考以下文章

python python_practice

python python_practice

python :practice function return

python: practice recurse function

python:practice module generator of list time jison

python:practice decorator calaulate_time