[Python] filter() 설명

filter() 함수는 iterables 안의 어떤 조건(함수)에 맞는 것만 골라내는 함수다. doc string을 보자.

filter(function or None, iterable) –> filter object

Return an iterator yielding those items of iterable for which function(item) is true. If function is None, return the items that are true.

어떤 iterables내의 item들 중 function에 참인 것만 골라서 iterator를 만든다. function에 None을 주면 true인 item들만 고른다. 예를 들면 다음과 같다.

names = [ 'Bamtol', 'Mong', 'Justin', 'Jay']
list(filter(lambda x: len(x) == 6, names))
['Bamtol', 'Justin']

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다