Python 3.1 快速導覽 - 內建函數 filter()

內建函數 (function) filter() ,參數 (parameter) function 為判斷的布林函數, iterable 則為迭代器,此函數取得依 function 篩選後 iterable 中的元素

函數描述
filter(function, iterable)function 為判斷的布林函數, iterable 則為迭代器,此函數取得依 function 篩選後 iterable 中的元素


舉例示範如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
def test(x):
    return x > 10
 
a = [11, 2, 8, 22, 6, 4, 15]
b = []
for i in filter(test, a):
    print(i)
 
# 《程式語言教學誌》的範例程式
# 檔名:filter.py
# 功能:示範 Python 程式
# 作者:張凱慶
# 時間:西元 2010 年 12 月


執行結果如下



中英文術語對照
函數function
參數parameter


內建函數




沒有留言: