{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## 28. 컴프리헨션 내부에 제어 하위 식을 세개 이상 사용하지 말라" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n" ] } ], "source": [ "matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n", "flat = [x for row in matrix for x in row]\n", "print(flat)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1, 4, 9], [16, 25, 36], [49, 64, 81]]\n" ] } ], "source": [ "squared = [[x**2 for x in row] for row in matrix]\n", "print(squared)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "만약 아래와 같은 경우 컴프리헨션 안에 다른 루프가 들어 있으면 코드가 너무 길어져 여러 줄로 나눠 써야 한다." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "my_lists = [\n", " [[1, 2, 3], [4, 5, 6]],\n", " [[7, 8, 9], [10, 11, 12]]\n", "]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "flat = [x for sublist1 in my_lists \n", " for sublist2 in sublist1\n", " for x in sublist2]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "flat" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "풀어 쓰는게 더 명확하다" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "flat = []\n", "for sublist1 in my_lists:\n", " for sublist2 in sublist1:\n", " flat.extend(sublist2)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "flat" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "컴프리헨션은 if 조건을 허용한다\n", "\n", "여러 조건을 같은 수준의 루프에 사용하면 암시적으로 and 식을 의미한다." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "b = [x for x in a if x > 4 if x % 2 == 0]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[6, 8, 10]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "c = [x for x in a if x > 4 and x % 2 == 0]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[6, 8, 10]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "filtered = [[x for x in row if x % 3 ==0]\n", " for row in matrix if sum(row) >= 10]" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[6], [9]]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "filtered" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "컴프리헨션에 들어가는 하위식이 세개 이상 되지 않게 제한하라!!\n", "\n", "즉 조건문 두개, 루프 두개 혹은 조건문 한개와 루프 한개를 사용하라" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 기억해야 할 내용\n", "- 컴프리헨션은 여러 수준의 루프를 지원하며 각 수준마다 여러 조건을 지원한다.\n", "- 제어 하위 식이 세 개 이상인 컴프리헨션은 이해하기 매우 어려우므로 가능하면 피해야한다." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 4 }