filter函數用法

filter(function, iterable)
function 爲 true 時從 iterable 的那些元素構造一個迭代器。 iterable可以是序列,支持迭代的容器,也可以是迭代器。 如果function爲None,即刪除所iterable的元素。

In [1]: list(filter(lambda x: True if x else False, [1, None, 2]))
Out[1]: [1, 2]

In [2]: list(filter(lambda x: x > 10, [11, 5, 2, 50]))
Out[2]: [11, 50]

In [3]: list(filter(lambda x: int(x) > 10, [11, 5, 2, "50"]))
Out[3]: [11, '50']
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章