# 리스트에 있는 모든 원소의 제곱을 계산한다고 하자.
# 간단한 for루프를 사용해 계산할 수 있다.
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
squares = []
for x in a:
squares.append(x**2)
print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
#리스트컴프리헨션을 사용하면 루프로 처리할 대상인
#입력 시퀀스의 원소에 적용할 변환식을 지정함으로써 같은 결과를 얻을 수 있음
squares = [x**2 for x in a] # 리스트 컴프리핸션
print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
#간단한 경우에도 map 내장 함수보다 리스트컴프리헨션이 더 명확하다.
alt = map(lambda x: x ** 2, a)
print(alt)
<map object at 0x0000016144ED3898>
# ex) 2로 나눠 떨어지는 수의 제곱만 계산하고 싶다.
# 이런 계산을 수행하려면 다음 코드처럼 리스트 컴프리헨션 루프 뒤 조건추가하면된다.
even_squares = [x**2 for x in a if x % 2 == 0]
print(even_squares)
[4, 16, 36, 64, 100]
#map을 이용하여 filter 내장함수를 map과 함께 사용해서 같은 결과를 얻을 수 있지만
#시각적으로 좋지않음.
alt = map(lambda x: x**2, filter(lambda x: x % 2 == 0, a))
assert even_squares == list(alt)
even_squares_dict = {x: x**2 for x in a if x % 2 == 0}
threes_cubed_set = {x**3 for x in a if x % 3 == 0}
print(even_squares_dict)
print(threes_cubed_set)
{2: 4, 4: 16, 6: 36, 8: 64, 10: 100}
{216, 729, 27}
#map과 filter를 이용하면 동일하게 만들 수 있지만 너무 늘어난다.
alt_dict = dict(map(lambda x: (x, x**2),
filter(lambda x: x % 2 == 0, a)))
alt_set = set(map(lambda x: x**3,
filter(lambda x: x % 3 == 0, a)))