체를 만들 때 외부에서 반복문을 돌리면서 이미 존재하는 리스트 객체에 원소를 추가하거나 수
정하는 것이 아니라 더 간결한 방식으로 반복문에 기반한 리스트를 직접 생성하는 방법이다.
In [117]: m = [i ** 2 for i in range(5)]
m
Out[117]: [0, 1, 4, 9, 16]
리스트 조건제시법은 반복문을 외부에 명시적으로 만들지 않기 때문에 어떤 의미로는 코드 벡
터화와 유사하다(코드 벡터화는
4
장과
5
장에서 자세히 다룬다).
3.2.4
함수형 프로그래밍
파이썬은 함수형 프로그래밍을 위한
filter
( )
,
map
( )
,
reduce
( )
와 같은 도구를 제공한다. 이
들을 사용하면 입력값에 대해 함수를 적용하는 작업을 효율적으로 할 수 있다. 우선 함수를 정
의해보자. 입력 변수
x
의 제곱을 출력하는 가장 간단한 함수
f
( )
를 만들어본다.
In [118]: def f(x):
return x ** 2
f(2)
Out[118]: 4
물론 복수의 입출력 인수를 가지는 훨씬 더 복잡한 함수를 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.
O’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
I wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
I’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
I'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.