python定義乙個函式使用def保留字,格式如下:
def 《函式名》(《引數》):
《函式體》
return 《返回值列表》
引數定義與傳遞的基本順序:位置引數->關鍵字引數->可變數量引數
普通的引數叫做位置引數,呼叫時需要按照位置傳遞引數值。
關鍵字引數就是在呼叫函式,傳入實參時指定形參的變數名。
當函式被呼叫時,如果沒有傳入對應的引數值,則使用函式定義時的預設值替代。可選引數必須定義在非可選引數的後面在函式定義時,也可以設計可變數量引數,通過引數前增加單星號(*)實現。帶有星號的可變引數只能出現在引數列表的後面。呼叫時,這些引數被當作元組型別傳遞到函式中。
在函式定義時,也可以通過在引數前增加帶雙星號向函式傳遞可變數量的引數。帶有雙星號的可變引數只能出現在引數列表的最後。呼叫時的那些引數被當作字典型別傳遞到函式中
>>> def test_fun(a,b=6,*c,**d):
print("a=",a,",b=",b,",c=",c,",d=",d)
>>> test_fun(1)
a= 1 ,b= 6 ,c= () ,d= {}
>>> test_fun(1,2)
a= 1 ,b= 2 ,c= () ,d= {}
>>> test_fun(1,2,3)
a= 1 ,b= 2 ,c= (3,) ,d= {}
>>> test_fun(1,2,3,4)
a= 1 ,b= 2 ,c= (3, 4) ,d= {}
>>> test_fun(1,2,3,4,c=3)
a= 1 ,b= 2 ,c= (3, 4) ,d=
>>> test_fun(1,2,3,4,5,6,x=1,y=2,z=3)
a= 1 ,b= 2 ,c= (3, 4, 5, 6) ,d=
使用lambda保留自定義,函式名是返回結果
該函式用於定義簡單的、能夠在一行內表示的函式
《函式名》 = lambda 《引數》 : 《表示式》
>>> f = lambda x,y,z:x+y+z
>>> f(1,1,1)
3>>> g = lambda x, y=2, z=3: x+y+z
>>> g(1)
6
zip()函式用於將可迭代的物件作為引數,將物件中對應的元素打包成乙個個元組,然後返回由這些元組組成的列表,返回的列表長度與最短的物件相同。
zip([iterable,...])
iterable:乙個或多個迭代器
返回結果:乙個物件(python3中)
>>> x = "abcd"
>>> y = "abcde"
>>> [i==j for i,j in zip(x,y)]
[true, true, true, true]
>>> zip(x,y)
>>> list(zip(x,y))
[('a', 'a'), ('b', 'b'), ('c', 'c'), ('d', 'd')]
*zip()函式是zip()函式的逆過程,將zip物件變成原先組合前的資料。
>>> x = "abcd"
>>> y = "abcde"
>>> zipped = zip(x,y)
>>> list(zip(*zipped))
[('a', 'b', 'c', 'd'), ('a', 'b', 'c', 'd')]
filter()用於過濾序列,過濾掉不符合條件的元素,返回由符合條件元素組成的新列表。
filter(function,iterable)
function:判斷函式
iterable:可迭代物件
返回結果:乙個物件
>>> a = filter(lambda x:x % 2 == 0,range(10))
>>> print(a)
>>> list(filter(none,[0,1,2,3,0,0]))
[1, 2, 3]
>>> list(filter(lambda x:x>2,[0,1,2,3,4]))
[3, 4]
reduce()函式會對引數序列中元素進行累積。
reduce(function,iterable[,initializer])
function:函式,有兩個引數
iterable:可迭代物件
initializer:可選,初始引數
返回結果:返回函式計算結果
>>> from functools import reduce
>>> def add(x,y):
return x + y
>>> reduce(add,[1,2,3,4,5])
15>>> from functools import reduce
>>> def add(x,y):
return x + y
>>> reduce(add,range(1,101))
5050
enumerate()用於將乙個可遍歷的資料物件組合為乙個索引序列,同時列出資料和資料下標,一般用在for迴圈當中。
enumerate(sequence,[start = 0])
sequence:乙個序列、迭代器或其他支援迭代物件
start:下標起始位置
返回結果:返回enumerate物件
>>> seasons = ["spring","summer","fall","winter"]
>>> list(enumerate(seasons))
[(0, 'spring'), (1, 'summer'), (2, 'fall'), (3, 'winter')]
>>> list(enumerate(seasons,start = 1))
[(1, 'spring'), (2, 'summer'), (3, 'fall'), (4, 'winter')]
>>> seq = ["tom","jerry","bob"]
>>> for i,element in enumerate(seq):
print(i,element)
0 tom
1 jerry
2 bob
map()根據提供的函式對指定序列做對映。
map(function,iterable)
function:函式
iterable:乙個或多個序列
返回結果:迭代器
>>> def square(x):
return x ** 2
>>> map(square,[1,2,3,4,5])
>>> def square(x):
return x ** 2
>>> list(map(square,[1,2,3,4,5]))
[1, 4, 9, 16, 25]
>>> list(map(str,[1,2,3]))
['1', '2', '3']
>>> list(map(lambda x:len(x),["a","bb","ccc"]))
[1, 2, 3]
天氣相關知識點彙總
1.數值天氣預報 numerical weather prediction 指根據大氣實際情況,在一定的初值和邊值條件下,通過大型計算機作數值計算,求解描寫天氣演變過程的流體力學和熱力學的方程組,未來一定時段的大氣運動狀態和天氣現象的方法。2.天氣預報分類 根據時效性長短 1 短時預報,根據雷達 衛...
hive相關綜合知識點彙總
一 檢視 檢視中保留有原表的元資料資訊,但是不會保留資料,當我們查詢檢視的資料的時候,可以查到,但是當我們查詢真正資料的時候,這是後才會去執行建立檢視的時候的sql語句。例如 表a為 執行建立檢視語句 create view a view as select from a where dt 2020...
相關知識點
nweb inf uclasses uweb.xml ulib n 從httpservlet 繼承,重寫doget dopost方法 n部署web.xml n 只有乙個物件 n 第一次請求的時候被初始化,只一遍 n 初始化後先呼叫init 方法,只一遍 n 每個請求,呼叫一遍service serv...