函数_global
global不放 相互之间的a没有关联
a=6688
def foo():
a=666
def inner_foo():
a=888
print('inner_foo ',a)
inner_foo()
print('foo ',a)
print(a)
foo()
print(a)
6688
inner_foo 888
foo 666
6688
global放函数的开头
a=6688
def foo():
global a
print('a:',a)
a=666
print('a:',a)
def bar():
print('bar(),a:',a)
foo()
bar()
a: 6688
a: 666
bar(),a: 666
global放内置嵌套函数里 glob引用在哪里就应用在哪里
a=6688
def foo():
a=666
def inner_foo():
global a
print('inner_foo change before ',a)
a=888
print('inner_foo change after ',a)
inner_foo()
print('foo ',a)
print(a)
foo()
print(a)
6688
inner_foo change before 6688
inner_foo change after 888
foo 666
888
列表元素可直接引用
li=['a','b']
def foo():
li.append('c')
print(li)
foo()
['a', 'b', 'c']
递归函数
在一个函数体的内部,调用函数本身,就被称为递归函数
def fuc(n):
if n == 1:
return 1
if n == 2:
return 1
else:
return fuc(n-1)+fuc(n-2)
print(fuc(10))
55
匿名函数(lambda)
- 格式
lambda para1,para2,...,paraN:expression using paras
demo:
f=lambda x,y,z:x+y+z
print(f(1,2,3))
print(type(f))
6
<class 'function'>
高阶函数
高阶函数:把一个函数名,以实参的形式,传递给这个函数的形参,这个函数就成为高阶函数
def add(a,b,c):
return c(a)+c(b)
a_value=add(-9,1,abs)
print(a_value)
10
def pow(x):
return x**2
def add(a,b,c):
return c(a)+c(b)
a_value=add(-9,1,pow)
print(a_value)
82
demo: 高阶函数与匿名函数结合
li=['Zhejiang','Unibersity','City','College']
f=lambda x:x.startswith('C')
f1=lambda x:x.endswith('ty')
def filter_test(para,func):
ret = []
for i in para:
if not func(i):
ret.append(i)
return ret
print(filter_test(li,f))
print(filter_test(li,f1))
['Zhejiang', 'Unibersity']
['Zhejiang', 'College']
filter函数
功能:
-
过滤掉序列中符合函数条件的元素。当序列中需要保留的元素可以用某些函数描述时,就应该想到filter函数
-
调用格式:
filter(function,sequence)
- function—>可以使自定义的函数,也可以是匿名函数
- sequence—>列表,元组,字符串
li=['Zhejiang','Unibersity','City','College'] f2=filter(lambda sr:not sr.endswith('ty'),li) print(list(f2)) ['Zhejiang', 'College'] # 互文数 f=filter(lambda li1 :str(li1)==str(li1)[::-1],range(1,1000)) print(list(f)) [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, ... 858, 868, 878, 888, 898, 909, 919, 929, 939, 949, 959, 969, 979, 989, 999]
map映射
功能
- 求一个序列或者多个序列进行函数映射后的值(用list()强转)
格式
map(function,iterable1,iterable2)
- function的参数可以不止一个
- iterable1,iterable2就是传入function的参数
li=[1,2,3,4,5]
res=map(lambda x:x+1,li)
print(list(res))
[2, 3, 4, 5, 6] # 自己执行遍历取出元素执行操作
x=[1,2,3,4]
y=[3,2,2,2]
def f(x,y):
return x*y
res=map(f,x,y)
print(list(res))
[3, 4, 6, 8]
x=[1,2,3,4]
y=[3,2,2,2]
res=map(lambda x,y:x*y+2,x,y)
print(list(res))
[5, 6, 8, 10]
reduce 函数
- 功能
- 对一个序列进行压缩运算,得到一个value。
- python2中,reduce()是内置函数,而现在,python3中,它被移植到functools模块中
from functools import reduce
- 格式
- reduce(function,iterable,[initial])
- fuction必须要传入两个参数
- iterable—>列表/元组
- initial—>初始值。如果加这个参数,则把这个参数当做第一个数开始运算,如果不加则取iterable中第一个元素作为初始值
- reduce(function,iterable,[initial])
from functools import reduce
y=[2,3,4,5,6]
z=reduce(lambda x,y:x+y,y) # 求和
# z=reduce(lambda x,y:10*x+y,y)
# z=reduce(lambda x,y:x*y,y) # 累乘
# 1 --->f(2,3)=5 --->[5,4,5,6]
# 2 --->f(5,4)=9 --->[9,5,6]
# 3 --->f(9,5)=14 --->[14,6]
# 4 --->f(14,6)=20
print(z)
20
# 23456
# 720
apply
功能
- pandas中,应用对象是pandas中的DataFrame或者Series
- 直接对DataFrame或者Series应用函数
- 对pandas中groupby之后的聚合对象应用apply
import numpy as np
import pandas as pd
a=np.random.randint(low=0,high=4,size=(2,4))
print(a)
data=pd.DataFrame(a)
print(data)
print(data.apply(lambda x:x*10))
[[2 2 1 1]
[1 1 3 1]]
0 1 2 3
0 2 2 1 1
1 1 1 3 1
0 1 2 3
0 20 20 10 10
1 10 10 30 10
zip
功能
- 将可迭代对象作为参数,将对象中对应的元素打包成一个个元组,返回由这些元组构成的对象
- 长度不一样的时候,以长度短的为准
注:
利用*号操作符,与zip相反,进行解压
格式:
zip(iterable1,iterable2,...)
-
iterable—>两个或者多个可迭代序列(字符串,列表,元组,字典)
- py2,返回由元组组成的列表
- py3,返回地址,需要用list强转
a=[1,2,3] b=[4,5,6] c=[4,5,6,7,8] ziptest=zip(a,b) #print(list(ziptest)) ziptest1=zip(*ziptest) # print(list(ziptest1)) # [(1, 4), (2, 5), (3, 6)] # 一次只能打印一次,取出表格为空 # [(1, 2, 3), (4, 5, 6)] a={1:11,2:22} b={3:33,4:33} c={4:44,5:44} print(list(zip(a,b,c))) # [(1, 3, 4), (2, 4, 5)] # 只保留键