[work] python---lambda、filter、引數的位置/關鍵字/收集/順序匹配
python —lambda
step2:lambda之再議
1.lambda是一個表示式。
2.它沒有名稱,儲存的也不是程式碼塊,而是表示式。
3.它被用作執行很小的功能,不能在裡面使用條件語句。
1
2
3
[email protected]:~/python/laowangpy/function# cat k501lambda.py
#!usr/bin/python
# --*-- coding:utf-8 --*--
d = lambda x:x+1
print d(3)
print d(4)
def e(x):#與lambda區別是有return返回
return x+1
e(8)
e(9)
[email protected]:~/python/laowangpy/function# python k501lambda.py
4
5
[email protected]:~/python/laowangpy/function#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
在lambda使用if……else…..語句
[email protected]:~/python/laowangpy/function# cat k501lambda.py
#!usr/bin/python
# --*-- coding:utf-8 --*--
d = lambda x:x+1 if x>0 else "error"
print d(3)
print d(4)
print d(-2)
[email protected]:~/python/laowangpy/function# python k501lambda.py
4
5
error
[email protected]:~/python/laowangpy/function#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
在lambda中使用列表推導
[email protected]:~/python/laowangpy/function# cat k501lambda.py
#!usr/bin/python
# --*-- coding:utf-8 --*--
'''
使用if....else....語句
d = lambda x:x+1 if x>0 else "error"
print d(3)
print d(4)
print d(-2)
'''
#使用列表推導
g = lambda x:[(x,i) for i in xrange(0,10)]
print g(1)
[email protected]:~/python/laowangpy/function# python k501lambda.py
[(1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9)]
[email protected]:~/python/laowangpy/function#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
結合filter使用過濾功能
[email protected]:~/python/laowangpy/function# cat k501lambda.py
#!usr/bin/python
# --*-- coding:utf-8 --*--
'''
使用if....else....語句
d = lambda x:x+1 if x>0 else "error"
print d(3)
print d(4)
print d(-2)
'''
'''
使用列表推導
g = lambda x:[(x,i) for i in xrange(0,10)]
print g(1)
'''
#結合filter使用過濾功能(常用過濾、判斷、查詢條件)
t = [2,3,4,5,61,2,34,52,1,1,2,3,7,1]
h = filter(lambda x:x>2,t)
print h
[email protected]:~/python/laowangpy/function# python k501lambda.py
[3, 4, 5, 61, 34, 52, 3, 7]
[email protected]:~/python/laowangpy/function#
以上題目的虛擬碼
h = []
for x in t:
if x>2:
h.append(x)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
結合filter使用過濾功能
>>> def shortstr(*kargs):
... list = filter(lambda x:isinstance(x,str),kargs)#判斷是否為字串
... len_list = [len(x) for x in list]#對字串長度判斷
... min_len = min(len_list)#取出最短的字串
... return list[len_list.index(min_len)]#返回最短的字串
...
>>> print shortstr(22,334,"xixi","hahahha","wowowo")
xixi
>>>
1
2
3
4
5
6
7
8
9
step3:函式引數總結
1.位置匹配 func(name)
2.關鍵字匹配 func(key=value)
3.收集匹配
1.元組收集 func(name,arg1,arg2)
2.字典收集 func(name,key1=value1,key1=value2)
4.引數順序
1
2
3
4
5
6
step4:接觸遞迴
1.遞迴是呼叫自身
2.理解下面的函式
1
2
位置匹配 func(name)(引數的位置一一對應)
def func(arg1,arg2,arg3):
return arg1,arg2,arg3
print func(1,2,3)
關鍵字匹配 func(key=value)
def func1(k1='',k2=None,k3=''):
return k1,k2,k3
print func1(k3=5,k1=4,k2=3)
收集匹配:1.元組收集 func(name,arg1,arg2)
2.字典收集 func(name,key1=value1,key1=value2)
* kargs 元組
** kwargs 字典
>>> def fun2(*kargs,**kwargs):
... return kargs
...
>>> print fun2(2,3,4,5,6,7,8)
(2, 3, 4, 5, 6, 7, 8)
>>> print fun2(2,3,4,5,6,7,8,[11,22,33,44,55,66],{111:222,333:444,555:666})
(2, 3, 4, 5, 6, 7, 8, [11, 22, 33, 44, 55, 66], {555: 666, 333: 444, 111: 222})
>>>
引數位置:
1.先是位置匹配的引數
2.再是關鍵字匹配的引數
3.收集匹配的元組引數
4.收集匹配的關鍵字引數
>>> def fun2(*kargs,**kwargs):
... return kargs
...
>>> print fun2(2,3,4,5,6,7,8)
(2, 3, 4, 5, 6, 7, 8)
>>> print fun2(2,3,4,5,6,7,8,[11,22,33,44,55,66],{111:222,333:444,555:666})
(2, 3, 4, 5, 6, 7, 8, [11, 22, 33, 44, 55, 66], {555: 666, 333: 444, 111: 222})
>>>
>>>
>>> def fun2(a,*kargs,**kwargs):
... return kargs
...
>>> print fun2(2,3,4,5,6,7,8,[11,22,33,44,55,66],{111:222,333:444,555:666})
(3, 4, 5, 6, 7, 8, [11, 22, 33, 44, 55, 66], {555: 666, 333: 444, 111: 222})
>>>
>>>
>>> def fun2(a,d,b=4,*kargs,**kwargs):
... return kargs
...
>>>
>>> print fun2(2,3,4,5,6,7,8,[11,22,33,44,55,66],{111:222,333:444,555:666})
(5, 6, 7, 8, [11, 22, 33, 44, 55, 66], {555: 666, 333: 444, 111: 222})
>>>
def func2(a,d,b=4,*kargs,**kwargs):
return kargs
print func2(2,3,4,5,6,7,9,[1,2,3,4],{1:2,3:4})
# d = lambda x:x+1 if x > 0 else "error"
# g = lambda x:[(x,i) for i in xrange(0,10)]
# def e(x):
# return x+1
# t = [1,2,3,4,5]
# g = filter(lambda x:x > 3,t)
# print d(2)
# print d(4)
# print d(5)
# print d(-1)
# print d(-2)
# #print g(10)
# print g
def func1(i):
if i<100:
return i + func1(i+1)
return i
print func1(0)
---------------------
作者:徐為波
來源:CSDN
原文:https://blog.csdn.net/xwbk12/article/details/78572766
版權宣告:本文為博主原創文章,轉載請附上博文連結!