[파이썬] 내장함수 filter
리스트에서 양수인 수만 출력하는 코드를 작성해보자 def positive(x): return x>0 print(list(filter(positive,[1,-2,2,0,-5,6]))) filter 함수를 사용해서 원하는 조건으로 리스트를 추출할 수 있다. 여기서 lambda를 이용하면 더욱 간편하게 코드를 작성할 수 있다. print(list(filter(lambda x:x>0,[1,-2,2,0,-5,6])))
2023. 2. 16.