python函数:函数参数,常用函数工具

版权声明:所有内容仅供大家学习与复习使用,请勿用于任何商业用途;维特根斯坦曾说过,凡可说的,皆无意义。凡有意义的,皆不得不以荒唐的语言传递其意义。我十分赞同。 https://blog.csdn.net/qq_40828914/article/details/86679986

>>> def f(a):
	a=99

	
>>> b=88
>>> f(b)
>>> print(b)
88
>>> def ch(a,b):
	a=2
	b[0]='eoe'

	
>>> x=1
>>> l=[1,2]
>>> ch(x,l)
>>> x,l
(1, ['eoe', 2])
>>> x=1
>>> a=x
>>> a=2
>>> print(x)
1
>>> l=[1,2]
>>> b=l
>>> b[0]='eoe'
>>> print(l)
['eoe', 2]
>>> l=[1,2]
>>> ch(x,l[:])
>>> l
[1, 2]
>>> l1=[2,3,4]
>>> l2=l1
>>> l1[0]=24
>>> l1,l2
([24, 3, 4], [24, 3, 4])
>>> l1=[2,3,4]
>>> l2=l1[:]
>>> l1[0]=24
>>> l1,l2
([24, 3, 4], [2, 3, 4])
>>> def ch(a,b):
	b=b[:]
	a=2
	b[0]='cho'

	
>>> x=1
>>> l=[1,2]
>>> ch(x,l)
>>> l
[1, 2]
>>> x
1
>>> def f(a,b,c):
	print(a,b,c)

	
>>> f(1,2,3)
1 2 3
>>> f(c=3,b=2,a=1)
1 2 3
>>> def f(a,b=2,c=3):
	print(a,b,c)

	
>>> f(1)
1 2 3
>>> f(a=1)
1 2 3
>>> f(1,4)
1 4 3
>>> f(6,7,8)
6 7 8
>>> f(1,c=9)
1 2 9
>>> def f(*args):print(args)

>>> f()
()
>>> f(1)
(1,)
>>> f(1,2,3,4)
(1, 2, 3, 4)
>>> def f(**args):print(args)

>>> f()
{}
>>> f(a=1,b=2)
{'a': 1, 'b': 2}
>>> def f(a,*b,**c):
	print(a,b,c)

	
>>> f(1,2,3,x=1,y=2)
1 (2, 3) {'x': 1, 'y': 2}
>>> def min1(*args):
	res=args[0]
	for arg in args[1:]:
		if arg < res:
			res=arg
	return res

>>> def min2(first,*res):
	for arg in res:
		if arg<first:
			first = arg
	return first

>>> def min3(*args):
	tmp=list(args)
	tmp.sort()
	return tmp[0]

>>> print(min1(4,3,5,1))
1
>>> print(min2('a','b','c','d'))
a
>>> print(min3([1,2,3],[3,2],[1,4,5]))
[1, 2, 3]
>>> def minmax(test,*args):
	res=args[0]
	for arg in args[1:]:
		if test(arg,res):
			res=arg
	return res

>>> def less(x,y):
	return x<y

>>> def great(x,y):
	return x>y

>>> print(minmax(less,1,3,2,4,6,7,5))
1
>>> print(minmax(great,1,3,2,4,6,7,5))
7
>>> def intersect(*args):
	res=[]
	for x in args[0]:
		for other in args[1:]:
			if x not in other:break
		else:
			res.append(x)
	return res

>>> def union(*args):
	res=[]
	for seq in args:
		for x in seq:
			if not x in res:
				res.append(x)
	return res

>>> s1,s2,s3='aaa','bbb','ccc'
>>> s1,s2,s3='aabbcdf','bcfff','aadsxxs'
>>> intersect(s1,s2),union(s1,s2)
(['b', 'b', 'c', 'f'], ['a', 'b', 'c', 'd', 'f'])
>>> intersect([1,2,3,4,5],(1,2,9,8))
[1, 2]
>>> intersect(s1,s2,s3)
[]
>>> union(s1,s2,s3)
['a', 'b', 'c', 'd', 'f', 's', 'x']
>>> def func(x,y,z):return x+y+z

>>> func(2,3,4)
9
>>> f=lambda x,y,z:x+y+z
>>> 
>>> f(2,3,4)
9
>>> x=(lambda a='aad',b='bbc',c='ccc':a+b+c)
>>> x('ddd')
'dddbbcccc'
>>> def kk():
	t='ook'
	ac=(lambda x : t+' '+x)
	return ac

>>> a=kk()
>>> a('ioi')
'ook ioi'
>>> L=[lambda x : x**2,lambda x : x**3,lambda x : x**4]
>>> for f in L:
	print(f(2))

	
4
8
16
>>> print(L[0](3))
9
>>> def f1(x):return x**2

>>> def f2(x):return x**3

>>> def f3(x):return x**4

>>> L=[f1,f2,f3]
>>> for f in L:
	print(f(2))

	
4
8
16
>>> print(L[0](3))
9
>>> key = 'qq'
>>> {'aa':(lambda:2+1),'bb':(lambda:2*2),'qq':(lambda:2*3)}[key]()
6
>>> def f1():return 2+2

>>> def f1():return 2+1

>>> def f2():return 2*2

>>> def f3():return 2*3

>>> key='qq'
>>> {'aa':f1,'bb':f2,'qq':f3}[key]()
6
>>> def ac(x):
	return (lambda y:x+y)

>>> act=ac(1)
>>> act(2)
3
>>> ac=(lambda x :(lambda y : x+y))
>>> a=ac(1)
>>> a(2)
3
>>> com=[1,2,3,4]
>>> da=[]
>>> for x in com:
	da.append(x+10)

	
>>> da
[11, 12, 13, 14]
>>> def inc(x):return x+10

>>> list(map(inc,com))
[11, 12, 13, 14]
>>> list(map((lambda x :x+10),com))
[11, 12, 13, 14]
>>> def mymap(func,se):
	res=[]
	for x in se:res.append(func(x))
	return res

>>> list(map(inc,[1,2,3]))
[11, 12, 13]
>>> mymap(inc,[1,2,3])
[11, 12, 13]
>>> list(map(pow,[1,2,3],[2,3,4]))
[1, 8, 81]
>>> list(range(-5,5))
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
>>> list(filter((lambda x : x>0),range(-5,5)))
[1, 2, 3, 4]
>>> re=[]
>>> for x in range(-5,5):
	if x >0:
		re.append(x)

		
>>> re
[1, 2, 3, 4]
>>> from functools import reduce
>>> reduce((lambda x,y:x+y),[1,2,3,4])
10
>>> reduce((lambda x,y:x*y),[1,2,3,4])
24
>>> L=[1,2,3,4]
>>> re=L[0]
>>> for x in L[1:]:
	re=re+x

	
>>> re
10

python函数之参数

1.参数共享和引用:

函数中对可变对象参数在原处修改能够影响调用者(列表,字典),但是对于不可变对象,将其赋值给一个参数,在函数内修改这个参数,对于这个对象本身没有影响。

2.避免可变参数修改:

python中,默认通过引用(引用)进行函数的参数传递。

如果是赋值语句:L1=[1,2,3] L2=L1 那么此时L2和L1引用了相同的对象。此时改变L1,L2也会跟着改变。

但如果是通过拷贝对象,而不是创建相同的引用,此时对L1的修改,不会影响L2的值。因为L2引用的是L1所引用对象的一个拷贝。

L1=[1,2,3] L2=L1[:]

因此,由上述可看出,我们在函数中避免可变参数修改有两种方法:

第一个就是在调用时对列表进行拷贝,另外一个就是直接在函数内部加一个拷贝语句。

4.python中的赋值:

等号左边是变量名,右边是对象,他们之间是通过引用进行联系的。

对于不可变对象来说,给一个变量赋一个新的值,并不是替换了原始对象,而是让这个变量去引用完全不同的一个对象。对一个变量赋值,仅仅会影响那个被赋值的变量。

a=3 b=a a='oo’此时输出b,仍然是3

4.关键字参数:

如果没有特殊的匹配语法,python默认通过位置从左到右匹配变量名。

如果有关键字参数,那么通过变量名进行匹配,而不是通过位置(参数从左至右的关系不再重要)。关键字参数起到了数据标签的作用,可读性增加

5.默认参数:

默认参数允许创建函数可选的参数。如果没有传入值的话,在函数运行前,参数就被赋了默认值。

6.接收任意数目参数:

有两个,第一个是*:将所有位置相关的参数收集到一个新元组里。

另一个是**:只对关键字参数有效,将关键字参数传递给一个新的字典里。

7.函数也能作为参数对象传入另一个函数。

python的lambda表达式:

1.lambda返回一个函数,lambda是一种生成函数对象的表达式,不是一个语句。只能在里面封装有限的逻辑。但是def则是在头部将一个新的函数赋值给一个变量名,而不是将这个函数作为结果返回。

2.默认参数也能够在lambda参数中使用。lambda主体中的代码遵循LEGB法则。

3.lambda表达式能够在使用的代码内嵌入一个函数的定义,使代码结构更为简洁。而且如果再利用字典,通过键索引来取回一个函数,相比if语句更为简洁。从而使字典成为多路分支工具

4.lambda出现在def中,在上层函数调用时候,嵌套的lambda能够获取上层函数作用域中变量名x的值。而且lambda也能够获取任意上层lambda中变量名。

函数工具:map,filter,reduce

1.map函数:对一个序列对象中每个元素应用被传入的函数,返回一个包含所有函数返回结果的一个列表。map想要一个函数传进来,这时候lambda表达式就可以写进去了。而且map里面还能够以多个序列作为参数,返回一个结果列表,每个序列中的元素分别对应函数的一个个参数。

2.filter函数:基于某一测试函数过滤出一些元素。

3.reduce函数:返回一个单一的结果,每个元素都运行函数,一般用于迭加或迭乘。

猜你喜欢

转载自blog.csdn.net/qq_40828914/article/details/86679986