python教程_小白入门2020/10/5
学习目标
文章目录
python教程_小白入门2020/10/5P125 filiter&map&reduce方法P126 内置函数总结P127 高阶函数1P128 高阶函数2
P125 filiter&map&reduce方法
from functools
import reduce
ages
= [12, 23, 30, 17, 16, 22, 19]
x
= filter(lambda ele
: ele
> 18, ages
)
print(x
)
adult
= list(x
)
print(adult
)
ages1
= [12, 23, 30, 17, 16, 22, 19]
m
= map(lambda ele
: ele
+ 2, ages1
)
print(list(m
))
scores
= [100, 89, 76, 87]
scores1
= [100, 89, 76, 87]
ss
= reduce(lambda ele1
, ele2
: ele1
+ ele2
, scores
)
print(ss
)
def foo(x
, y
):
return x
+ y
print(reduce(foo
, scores1
))
students
= [
{'name': 'zhangsan', 'age': 22, 'score': 98, 'height': 180},
{'name': 'lisi', 'age': 21, 'score': 97, 'height': 185},
{'name': 'jack', 'age': 22, 'score': 100, 'height': 175},
{'name': 'tony', 'age': 23, 'score': 90, 'height': 176},
{'name': 'herry', 'age': 20, 'score': 95, 'height': 172}
]
print(reduce(lambda xxx
, yyy
: xxx
+ yyy
['age'], students
, 0))
P126 内置函数总结
print(all(['hello', 'good', 'yes']))
print(all(['hello', 0]))
print(any(['hello', 'good', 'yes']))
print(any(['hello', 0]))
nums
= [1,2,3]
print(dir(nums
))
print(dir('hello'))
shang
,yushu
= divmod(15,2)
print(shang
,yushu
)
P127 高阶函数1
def foo():
print("我是foo,我被调用了")
return 'foo'
def doo():
print('我是doo,我被调用了')
return foo
()
doo
()
x
= doo
()
print(x
)
def aoo():
print("我是aoo,我被调用了")
return 'aoo'
def boo():
print('我是boo,我被调用了')
return aoo
y
= boo
()
print(y
)
y
()
boo
()()
z
= boo
()()
print(z
)
def outer():
m
=100
def inner():
n
= 90
print("我是inner函数")
print("我是outer函数")
return inner
outer
()()
P128 高阶函数2
def test():
print("我是test函数")
return 'hello'
def demo():
print("我是demo函数")
return test
def bar():
print("我是bar函数")
return test
()
x
= test
()
print(x
)
y
= demo
()
y
()
z
= bar
()
print(z
)
有括号就是代表调用了函数,没括号就是代表返回的值是这个函数,返回的是函数类型。