做過swift
開發的童鞋都知道, 在swift
中有許多的高階函式(map
,filter
,reduce
,zip
等), 這些在開發中讓我們節省大量**
python
中同樣有許多的內建函式, 但是這裡也只介紹幾個常用的高階函式
根據提供的函式對指定序列做對映, 並返回對映後的序列
map(function, iterable, ...)
引數/返回值
使用示例
# 呼叫外部函式
defsquare
(x):
return x ** 2
res = map(square, [1, 2, 3, 4])
print(res)
print(list(res))
# 使用匿名函式
res1 = map(lambda x: x * 3, [1, 2, 3, 4])
print(list(res1))
# 使用內建函式
res2 = map(str, [2, 3, 4, 5])
print(list(res2))
# 多個序列
res3 = map(lambda x, y: x * y, [1, 2, 3, 4], [3, 2, 4, 1])
print(list(res3))
'''輸出結果
[1, 4, 9, 16]
[3, 6, 9, 12]
['2', '3', '4', '5']
[3, 4, 12, 4]
'''
reduce(function, iterable
[, initializer])
引數/返回值
使用示例
# 求元素的和
defmysum
(x, y):
return x + y
list1 = [1, 2, 3, 4]
red = reduce(mysum, list1)
print(red)
red2 = reduce(mysum, list1, 2)
print(red2)
# 匿名函式
red3 = reduce(lambda x, y: x * y, list1)
print(red3)
red4 = reduce(lambda x, y: x * y, list1, 3)
print(red4)
red5 = reduce(lambda x, y: x + y, ['1', '2', '3', '4'], '數字: ')
print(red5)
'''輸出結果:
1012
2472
數字: 1234
'''
用於過濾序列,過濾掉不符合條件的元素,返回由符合條件元素組成的新列表
filter(function, iterable)
引數/返回值
使用示例
def
isodd
(x):
if x % 2 == 1:
return
true
return
false
list2 = [1, 2, 3, 4, 5, 6]
fil0 = filter(isodd, list2)
print(fil0)
print(list(fil0))
# 匿名函式
fil = filter(lambda x: x % 2 == 0, list2)
print(list(fil))
'''輸出結果:
[1, 3, 5]
[2, 4, 6]
'''
list
.sort(cmp=
none, key=
none, reverse=
false)
sorted(iterable[, cmp[, key[, reverse]
]])
引數/返回值
使用示例
使用sort()
排序
# 用sort
list3 = [3, 7, 2, 5, 0, 4]
list3.sort()
print(list3)
alist = ['123', 'google', 'runoob', 'taobao', 'facebook']
alist.sort(reverse=true) # 降序
print(alist)
deftakesecond
(elem):
return elem[1]
random = [(2, 2), (3, 4), (4, 1), (1, 3)]
# 指定第二個元素排序
random.sort(key=takesecond)
print(random)
'''輸出結果:
[0, 2, 3, 4, 5, 7]
['taobao', 'runoob', 'google', 'facebook', '123']
[(4, 1), (2, 2), (1, 3), (3, 4)]
'''
使用sorted()
函式排序時
list4 = sorted(list3)
print(list4)
#按絕對值大小排序
list5 = [4,-7,2,6,-3]
#key接受函式來實現自定義排序規則
list6 = sorted(list5, key=abs)
print(list6)
# 將序排列
print(sorted(list5, key=abs, reverse=true))
#函式可以自己寫
defmylen
(str):
return len(str)
list7 = ['b333','a1111111','c22','d5554']
list8 = sorted(list7, key=mylen) # 預設公升序排序
print(list8)
# 匿名函式
list9 = sorted(list7, key=lambda x: len(x), reverse=true)
print(list9)
'''輸出結果:
[0, 2, 3, 4, 5, 7]
[2, -3, 4, 6, -7]
[-7, 6, 4, -3, 2]
['c22', 'b333', 'd5554', 'a1111111']
['a1111111', 'd5554', 'b333', 'c22']
'''
enumerate(sequence, [start=0])
引數/返回值
使用示例
seasons = ['spring', 'summer', 'fall', 'winter']
sea1 = enumerate(seasons)
print(sea1)
print(list(sea1))
# 自定義起始索引
sea2 = list(enumerate(seasons, start=1))
print(sea2)
# 普通的 for 迴圈
i = 0
for element in seasons:
print(i, seasons[i])
i +=1
# for 迴圈使用 enumerate
for i, ele in enumerate(seasons):
print(i, ele)
'''輸出結果:
[(0, 'spring'), (1, 'summer'), (2, 'fall'), (3, 'winter')]
[(1, 'spring'), (2, 'summer'), (3, 'fall'), (4, 'winter')]
0 spring
1 summer
2 fall
3 winter
0 spring
1 summer
2 fall
3 winter
'''
zip([iterable, ...])
使用示例a = [1, 2, 3]
b = ['a', 'b', 'c']
c = [4, 5, 6, 7]
zip1 = zip(a, b)
print(zip1)
print(list(zip1))
# 兩個列表不同元素個數, 元素個數與最短的列表一致
zip2 = zip(a, c)
print(list(zip2))
# `*`號操作符,可以將元組解壓為列表
a1, c1 = zip(*zip(a, c))
print(a1)
print(c1)
'''輸出結果:
[(1, 'a'), (2, 'b'), (3, 'c')]
[(1, 4), (2, 5), (3, 6)]
(1, 2, 3)
(4, 5, 6)
'''
list31 = [1, 2, 3, 4]
list31.reverse()
print(list31)
# 輸出:
[4, 3, 2, 1]
Python之高階函式
一 什麼是高階函式 函式作為實參傳遞給函式的或者函式名為返回值的函式稱為高階函式。1 實參傳遞給函式 2 函式名為返回值 二 系統內建的高階函式 1 map函式 至少需要兩個引數,第乙個引數是函式名,第二個引數是序列 str,list,tuple map功能 把序列中的每乙個元素作為引數,傳給函式進...
python之高階函式
函式程式設計及其優勢 無 不修改狀態,表示式形式,專注與計算,接近自然語言。便於 熱公升級,無狀態不用考慮併發過程中的資源搶占及鎖問題。函式也是物件,也可以賦值給變數,當然函式的引數也可以為另一函式。map 是 python 內建的高階函式,它接收乙個函式 f 和乙個 list,並通過把函式 f 依...
python之高階函式
abs與abs 的區別 abs 返回值 abs 返回整個函式 求絕對值 print 求出來的絕對值為 abs 11 函式本身可以賦值給變數,變數也可以指向函式 呼叫函式來求絕對值 f abs print 求出來的絕對值為 f 10 傳遞的函式包括函式名 def fun x,y,f return f ...