码迷,mamicode.com
首页 > 编程语言 > 详细

python:practice built-in function

时间:2020-03-15 11:23:37      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:cal   bin   factor   ali   too   seq   fun   turn   effect   

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

标签:cal   bin   factor   ali   too   seq   fun   turn   effect   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!