{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 03wk-2: 리스트는 쓰레기인가?\n", "\n", "최규빈 \n", "2024-03-22\n", "\n", "\n", "\n", "# 1. 강의영상\n", "\n", "\n", "\n", "# 2. Imports" ], "id": "b878b884-8d8a-489f-b4c6-7240f8b030a6" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt " ], "id": "c9057763-4fe0-485d-b00a-e74ded37f5a6" }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3. 리스트는 쓰레기인가?\n", "\n", "## A. 벡터화연산(브로드캐스팅) 불가능\n", "\n", "`-` 벡터화연산 불가능 (최악의 단점)" ], "id": "db4686a9-1b8b-4077-aaa5-d39f8cbfa42b" }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "lst = [1,2,3]\n", "lst + 1" ], "id": "b9306300-9ce4-4ada-a8fa-3c64fe60d52a" }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "arr = np.array([1,2,3])\n", "arr + 1" ], "id": "57266e27-d2f3-4da3-b282-548b8c39c756" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## B. bool을 이용한 인덱싱이 불가능\n", "\n", "`-` True, False 가 포함된 array를 이용한 인덱싱이 불가능하다.\n", "\n", "`-` 넘파이에서 bool을 이용한 인덱싱" ], "id": "42041280-5b57-4311-a832-27d51078bb15" }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "arr = np.array([1,2,3,4,5])\n", "arr[arr>3]" ], "id": "ab5eee03-d8ad-4975-840b-34b90a98a9cb" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 리스트는 불가능" ], "id": "0a6b5783-cec3-4f37-8940-34c59509fc12" }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "lst = [1,2,3,4,5]\n", "lst[lst>3]" ], "id": "651c72aa-5652-43d8-93b5-bb17c1369f65" }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "lst>3" ], "id": "4b89aa6a-9f3d-4c3f-8cb1-927a6405d258" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## C. 넘파이 특화 메소드 사용불가능\n", "\n", "`-` 넘파이 특화 메소드" ], "id": "03dd9df7-f556-40cb-928e-5b44834db100" }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "arr = np.array([5,4,3,2,1])\n", "arr" ], "id": "b8e8cda7-e55b-4652-8cd1-2d33fdd1b655" }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "arr.min(), arr.max(), arr.mean(), arr.argmin(), arr.argmax()" ], "id": "566c0e1a-3e1d-4628-8704-3b43f36a6cbe" }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "arr.sort()\n", "arr" ], "id": "debd202c-89ce-4891-9d63-564a4fd51858" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 리스트는?" ], "id": "24f4184a-d4a1-4370-81cd-8a35c2140295" }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "lst = [5,4,3,2,1]\n", "lst" ], "id": "e62094a7-43a7-4f8b-8271-586432a4a857" }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "lst.min(), lst.max(), lst.mean(), lst.argmin(), lst.argmax()" ], "id": "ba60bcdc-7cbd-4b4e-becb-61f9e8e93a8a" }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "lst.sort()\n", "lst" ], "id": "ffe529d4-fefc-484e-9f5f-72ddfcab4bc0" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`# 메소드란?` – 자료형에 종속된 특수기술" ], "id": "f0112ead-5d09-4f3b-8d95-6833232f6c05" }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "# 넘파이 특수기술\n", "arr = np.array([1,2,3])\n", "arr.max() # max(arr)로 해석" ], "id": "be9764bd-d550-4db2-9fcb-3082819c13d6" }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "# ?.max() 이런건 넘파이 특수기술 이니까 ?자리에 리스트일 경우는 실행안됨\n", "lst = [1,2,3]\n", "lst.max() " ], "id": "8affecab-1d54-406b-8176-d2296b39e47d" }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "# 리스트의 특수기술 \n", "lst = [1,2,3]\n", "lst.append(4) # append(lst,4) 로 해석\n", "lst" ], "id": "bd0e85a5-ec03-4bfd-a589-1b421ac1cee7" }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "# 문자열 특수기술\n", "s = 'asdf'\n", "s.capitalize() # capitalize(s) 로 해석" ], "id": "4d94db36-8c67-43ee-9d1e-9694f5fe68d5" }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "# 문자열 특수기술\n", "s = '-'\n", "lst = ['x','y','z']\n", "s.join(lst) # join(s,lst) 로 해석" ], "id": "c5dc6fc7-6f37-4638-9d2b-b9aa1a2dee60" }, { "cell_type": "markdown", "metadata": {}, "source": [ "- `join(s,lst)`의 뜻: lst의 각 원소를 모두 “이어서” 하나의 문자열로\n", " 만들어라. 단, 원소간의 구분은 `s`에 저장된 문자열로 하라." ], "id": "7ab17217-83c1-47c6-99d1-2fcb31e11e21" }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [], "source": [ "# 문자열 특수기술 응용\n", "''.join(['x','y','z'])" ], "id": "d28ced3e-37d8-425e-b485-22ead4b081bf" }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [], "source": [ "# 문자열 특수기능 \n", "s = \"제 이름은 {} 입니다.\" \n", "name = \"최규빈\"\n", "s.format(name) # format(s,name)" ], "id": "c395fe77-3efe-4599-9a6a-4e6c1a8f1c8e" }, { "cell_type": "markdown", "metadata": {}, "source": [ "- `format(s,name)`의 뜻: s라는 문자열의 포맷에 `{}` 자리에 name을\n", " 끼워넣어라.\n", "\n", "## D. 파이썬에서 쓰레기 같은 자료형은 없다.\n", "\n", "`-` 파이썬에는 각 자료형마다 사용할 수 있는 고유기술(=메소드)이나 문법이\n", "있음.\n", "\n", "`-` 내가 생각할 때 리스트는 필요없는 자료형이야 = 난 리스트만 가지고\n", "있는 어떠한 고유특징을 활용하지 못해.\n", "\n", "`-` 파이썬을 잘 하려면 자료형에 따른 고유 특징을 이해하고 활용할 줄\n", "알아야 한다.\n", "\n", "- 기본자료형: str, list, tuple, dict, set – 이러한 자료형을 이해하고\n", " 기능을 활용해야함\n", "- 모듈: 넘파이배열, 판다스\n", "\n", "# 4. 2024년 수능 – 확통 23\n", "\n", "## A. 문제파악\n", "\n", "![](https://github.com/guebin/PP2024/blob/main/imgs/2024-수능-확통-23.png?raw=true)\n", "\n", "`-` 기존의 문제들과 다르게 numpy를 이용하여 풀기는 어려울 것 같다. 뭔가\n", "기존문제들과 결이 다름\n", "\n", "`-` 그런데 코딩으로 해결가능할 것 같긴 함\n", "\n", "## B. 슈도알고리즘\n", "\n", "`1.` 편의상 $[x,x,y,y,z]$를 $[x_1,x_2,y_1,y_2,z]$와 같이 생각하고\n", "나열한다. (5! = 120 개만큼 경우가 나열되겠지)\n", "\n", "`2.` 중복을 제거한다. 즉 아래는 모두 같은 경우로 생각한다.\n", "\n", "- $[x_1,x_2,y_1,y_2,z]$\n", "- $[x_1,x_2,y_2,y_1,z]$\n", "- $[x_2,x_1,y_1,y_2,z]$\n", "- $[x_2,x_1,y_2,y_1,z]$\n", "\n", "## C. 예비학습\n", "\n", "`# 개념1` – 집합이라는 자료형이 있음." ], "id": "d98796b8-ee64-4a3a-854e-2cb0e5ced513" }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "a = {1,2,3}\n", "type(a)" ], "id": "432033f4-0afd-40cc-8023-3e5863963871" }, { "cell_type": "markdown", "metadata": {}, "source": [ "그런데 집합은 중복된 원소를 포함하지 않았음 (중학교때 배운듯)" ], "id": "29fe2dd3-f1ce-4afd-bcec-898b37240eec" }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "a = {1,2,3,3,3}\n", "a" ], "id": "1542e21c-d687-4c48-9589-dc457ae44d3f" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "`# 개념2` – 자료형변환을 이용하여 중복된 원소를 제거\n", "\n", "아래와 같은 자료형이 있다고 하자." ], "id": "f10e47cd-5f9f-4688-a542-1069d3daa785" }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "lst = [1,2,3,3,3] \n", "lst" ], "id": "e369db23-87be-4644-8159-40ae41dcb678" }, { "cell_type": "markdown", "metadata": {}, "source": [ "중복된 것을 제외하고 싶다면?" ], "id": "ab59c6fc-d486-4c47-b452-5690d4d22e68" }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "list(set(lst)) # 자료형변환" ], "id": "7f19ec5a-9ec6-4051-83f0-c774cd7c57cb" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "`# 개념3` – 고유의 원소 숫자 세기" ], "id": "2961b590-62e0-4205-8528-d0d5a0d180cb" }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "lst = [1,2,3,3,3,4,4,5,5,6] \n", "lst" ], "id": "9e5243d9-df46-42db-8ce9-70d7b05c5435" }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "len(set(lst))" ], "id": "468b766c-29a0-4b4a-a599-96a976050afa" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "`# 개념4` – for문\n", "\n", "아래를 출력하고 싶다고 하자.\n", "\n", " 이효리는 핑클의 멤버이다. \n", " 옥주현은 핑클의 멤버이다.\n", " 성유리는 핑클의 멤버이다. \n", " 이진은 핑클의 멤버이다. \n", "\n", "(풀이1) – 단순한 풀이" ], "id": "937af0d5-f73c-40fd-8438-b5ff9bab07cf" }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "이효리는 핑클의 멤버이다.\n", "옥주현은 핑클의 멤버이다.\n", "성유리는 핑클의 멤버이다.\n", "이진은 핑클의 멤버이다." ] } ], "source": [ "print(\"이효리는 핑클의 멤버이다.\")\n", "print(\"옥주현은 핑클의 멤버이다.\")\n", "print(\"성유리는 핑클의 멤버이다.\")\n", "print(\"이진은 핑클의 멤버이다.\")" ], "id": "3eaadf19-ba36-4104-b657-3cf477036d1f" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(풀이2) – 문자열 특수기능을 이용해볼까?\n", "\n", "“{} 핑클의 멤버이다.” 이 공통포맷이므로, 아래와 같이 수행할 수 있겠다." ], "id": "32602df8-9784-4503-a590-4f393e466326" }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "이효리는 핑클의 멤버이다.\n", "옥주현은 핑클의 멤버이다.\n", "성유리는 핑클의 멤버이다.\n", "이진은 핑클의 멤버이다." ] } ], "source": [ "lst = [\"이효리는\",\"옥주현은\",\"성유리는\",\"이진은\"]\n", "i = 0\n", "print(\"{} 핑클의 멤버이다.\".format(lst[i]))\n", "i = 1 \n", "print(\"{} 핑클의 멤버이다.\".format(lst[i]))\n", "i = 2 \n", "print(\"{} 핑클의 멤버이다.\".format(lst[i]))\n", "i = 3 \n", "print(\"{} 핑클의 멤버이다.\".format(lst[i]))" ], "id": "73851a24-7f46-4882-8c4d-fe1d3f782f96" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(풀이3) - for와 문자열 특수기능을 이용해볼까?" ], "id": "60347948-1b8c-4270-ac67-b0756086f917" }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "이효리는 핑클의 멤버이다.\n", "옥주현은 핑클의 멤버이다.\n", "성유리는 핑클의 멤버이다.\n", "이진은 핑클의 멤버이다." ] } ], "source": [ "lst = [\"이효리는\",\"옥주현은\",\"성유리는\",\"이진은\"]\n", "for i in [0,1,2,3]:\n", " print(\"{} 핑클의 멤버이다.\".format(lst[i]))" ], "id": "22e0229b-a0e0-4518-bef8-e6fb8aa73d49" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(풀이4) - for와 문자열 특수기능을 이용해볼까? (2)\n", "\n", "`[0,1,2,3]`은 대충 `range(4)`와 비슷한 것이므로 아래와 같이 수행할 수도\n", "있겠음." ], "id": "9c1a6dd6-23d4-4b45-b69b-b1e69de9f401" }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "이효리는 핑클의 멤버이다.\n", "옥주현은 핑클의 멤버이다.\n", "성유리는 핑클의 멤버이다.\n", "이진은 핑클의 멤버이다." ] } ], "source": [ "lst = [\"이효리는\",\"옥주현은\",\"성유리는\",\"이진은\"]\n", "for i in range(4):\n", " print(\"{} 핑클의 멤버이다.\".format(lst[i]))" ], "id": "9bf99f97-4b5c-47a7-9f2d-51400ca38935" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(풀이5) - for와 문자열 특수기능을 이용해볼까? (3)\n", "\n", "아래의 코드는 $i$ 자리에 `[0,1,2,3]`의 원소가 번갈아 대입되며 `??????`가\n", "수행되었음.\n", "\n", "``` python\n", "lst = [\"이효리는\",\"옥주현은\",\"성유리는\",\"이진은\"]\n", "for i in [0,1,2,3]:\n", " ??????\n", "```\n", "\n", "아래의 코드도 $i$ 자리에 `range(4)`의 원소가 번갈아 대입되며 `??????`가\n", "수행되었다고 해석가능.\n", "\n", "``` python\n", "lst = [\"이효리는\",\"옥주현은\",\"성유리는\",\"이진은\"]\n", "for i in range(4):\n", " ??????\n", "```\n", "\n", "아래의 코드는 어떻게 실행될까??\n", "\n", "``` python\n", "lst = [\"이효리는\",\"옥주현은\",\"성유리는\",\"이진은\"]\n", "for i in lst:\n", " ??????\n", "```" ], "id": "b64b2648-f490-458e-bbc2-f6059f238eea" }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "이효리는 핑클의 멤버이다.\n", "옥주현은 핑클의 멤버이다.\n", "성유리는 핑클의 멤버이다.\n", "이진은 핑클의 멤버이다." ] } ], "source": [ "# R과 다른 방식으로 동작하는 for문 \n", "lst = [\"이효리는\",\"옥주현은\",\"성유리는\",\"이진은\"]\n", "for l in lst:\n", " print(\"{} 핑클의 멤버이다.\".format(l))" ], "id": "a387a652-9f1e-462f-81cf-ff299dc8488b" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "`# 개념5` – for문을 이용하여 리스트의 원소 추가하기" ], "id": "07e79395-5800-4c14-9850-7d6fd3b183dd" }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [], "source": [ "lst2 = [] # lst2 = list() 와 같은결과임\n", "lst = [\"이효리는\",\"옥주현은\",\"성유리는\",\"이진은\"]\n", "for l in lst: \n", " lst2.append(\"{} 핑클의 멤버이다.\".format(l))" ], "id": "8bc1d2d1-04d4-430d-a56d-0f143a444d5b" }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [], "source": [ "lst2" ], "id": "b00f9616-8943-4cf0-829d-3d2eb800642c" }, { "cell_type": "markdown", "metadata": {}, "source": [ "그런데 이걸 아래와 같이 해결할 수도 있다." ], "id": "b422b7fc-cf82-4049-a19f-04944ae29741" }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [], "source": [ "lst = [\"이효리는\",\"옥주현은\",\"성유리는\",\"이진은\"]\n", "[\"{} 핑클의 멤버이다.\".format(l) for l in lst] # 리스트컴프리헨션 " ], "id": "b8117153-ac96-4311-982b-c1a9473da9b6" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "`# 개념6` – 리스트컴프리헨션\n", "\n", "집합을 표현하는 방법에는 원소나열법과 조건제시법이 있다.\n", "\n", "- 원소나열법: $\\{2^0,2^1,2^2,2^3\\}$\n", "- 조건제시법: $\\{2^i: \\text{ for } i=0,1,2,3\\}$\n", "\n", "이중에서 조건제시법은 아래와 같이 표현할 수 있음.\n", "\n", "- $\\{2^i: \\text{ for } i \\in \\{0,1,2,3\\}\\}$\n", "\n", "여기에서 $\\in$ 은 `in` 으로 읽으므로, 위의 표기법을 연상하여 파이썬\n", "코드로 바꿔보면\n", "\n", "``` python\n", "[2^i: for i in [0,1,2,3]]\n", "```\n", "\n", "와 같은 방식으로 리스트의 원소를 표현할 수 있을 것 같다. 위의 코드는\n", "실행되지 않지만 아래의 코드는 실행가능하다." ], "id": "de65179a-d8a1-4f39-8be9-049faf9bb192" }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [], "source": [ "[2**i for i in [0,1,2,3]]" ], "id": "cf078aee-0b4d-4f11-997c-0a40c2134885" }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [], "source": [ "[2**i for i in range(4)]" ], "id": "5c459a7c-bd80-41c0-ab65-0cef3a6be910" }, { "cell_type": "markdown", "metadata": {}, "source": [ "이를 응용하면" ], "id": "32381ddf-e172-401a-bef3-83823bf9f65c" }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [], "source": [ "[\"asdf{}\".format(2**i) for i in range(4)]" ], "id": "925c5090-873f-490d-b458-4e4c3ac449f6" }, { "cell_type": "markdown", "metadata": {}, "source": [ "이것을 다시 응용하면" ], "id": "63739eee-f4a8-4ab4-b5fb-b2f3033b42df" }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [], "source": [ "lst = [\"이효리는\", \"성유리는\", \"옥주현은\", \"이진은\"]\n", "[\"{} 핑클의 멤버이다.\".format(l) for l in lst]" ], "id": "ed2d30c9-cf36-4a1b-b196-86c8dc69176c" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "`# 개념7` – 스트링의 인덱싱 (파이썬에서는 스트링을 array로 취급)" ], "id": "ff24fbb9-89e2-4ae6-a14f-2d5737f93cf1" }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [], "source": [ "s = 'asdf'\n", "s" ], "id": "987db9e8-1c6b-4271-b7ec-bbf9379cdc50" }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [], "source": [ "s[0]" ], "id": "54a3806e-ea28-41c4-a52b-0290ba330ec6" }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [], "source": [ "s[-1]" ], "id": "5fd32263-b5bd-4b04-baf6-5b7f0e1f2986" }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [], "source": [ "s[:2]" ], "id": "9cfbed91-2ffd-4a16-bb01-06f488ef6ec6" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "`# 개념8` – 스트링과 리스트의 변환" ], "id": "073b3276-a304-43c1-ac06-65371d3d7ad9" }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [], "source": [ "s = 'asdf' \n", "s" ], "id": "a19d3f72-648a-409a-9ad5-1318a038dbdf" }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [], "source": [ "lst = list(s)\n", "lst" ], "id": "862aeabd-c5d4-4b45-8eb5-c4d527b931e9" }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [], "source": [ "''.join(lst)" ], "id": "d04e39bc-48a3-4eda-a621-ff71fce276bc" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`# 개념9` – 튜플자료형" ], "id": "b9f4dc13-e321-430d-a92b-03bb9ea2af61" }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [], "source": [ "lst = [1,2,3]\n", "lst" ], "id": "39f85ed4-5630-4cd9-be87-ad514803d3aa" }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [], "source": [ "tpl = (1,2,3)\n", "tpl" ], "id": "cf47fab9-3d7f-4e52-88cb-10ff034352fa" }, { "cell_type": "markdown", "metadata": {}, "source": [ "튜플은 (의미가 명확할때) 괄호를 생략할 수 있음" ], "id": "21d4feb7-40e2-4f10-bb0e-1047398d608c" }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [], "source": [ "tpl = 1,2,3\n", "tpl" ], "id": "9f4015c0-446b-466f-bb2e-896305a819a1" }, { "cell_type": "markdown", "metadata": {}, "source": [ "튜플은 리스트와 매우 비슷함." ], "id": "c019f7e3-2b48-4322-8ff6-39ce06919513" }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [], "source": [ "[1,2,3] + [4,5] " ], "id": "d649e7d9-e7e9-4073-9a89-1943a3c403d5" }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [], "source": [ "(1,2,3) + (4,5)" ], "id": "c412094b-0364-42cf-b254-a04243401c51" }, { "cell_type": "markdown", "metadata": {}, "source": [ "아래도 가능" ], "id": "20b1b4c5-c0ae-49ef-92ed-5f7e7ef2831a" }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [], "source": [ "''.join(['a','b','c'])" ], "id": "7ba7f8d6-e684-4ad7-bdd4-f41daf73580b" }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [], "source": [ "''.join(('a','b','c'))" ], "id": "4905dce6-18e2-4562-b820-d928b91c0f87" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "`# 개념10` – for를 수행하는 다양한 테크닉\n", "\n", "**우리가 알고 있는 `for`**: 리스트 비슷한 것을 만든 뒤, **그 리스트의\n", "원소를 하나씩 뽑아가면서** 어떠한 반복구문 “??????” 수행하는 것." ], "id": "1e9cf4f7-2836-45b2-94a9-d39346ba68ff" }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [], "source": [ "import itertools" ], "id": "cd3ce647-d7e5-4ffa-b2b5-bb25a4cc7af6" }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "('a', 'b', 'c')\n", "('a', 'c', 'b')\n", "('b', 'a', 'c')\n", "('b', 'c', 'a')\n", "('c', 'a', 'b')\n", "('c', 'b', 'a')" ] } ], "source": [ "for i in itertools.permutations(['a','b','c']):\n", " print(i)" ], "id": "04743e92-835c-4853-a388-970b08640a17" }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "('x', 'a')\n", "('x', 'b')\n", "('x', 'c')\n", "('y', 'a')\n", "('y', 'b')\n", "('y', 'c')" ] } ], "source": [ "for i in itertools.product(['x','y'],['a','b','c']):\n", " print(i)" ], "id": "11d0ab34-dbac-41ab-8408-6a236308fb7b" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "## D. 풀이\n", "\n", "아래의 문제를 풀어보자..\n", "\n", "![](https://github.com/guebin/PP2024/blob/main/imgs/2024-수능-확통-23.png?raw=true)\n", "\n", "(풀이)" ], "id": "e6c8159b-8a09-4a55-89a2-b5dab0db9018" }, { "cell_type": "code", "execution_count": 144, "metadata": {}, "outputs": [], "source": [ "len(set([''.join(i) for i in itertools.permutations(['x','x','y','y','z'])]))" ], "id": "c586a390-182c-494a-bf72-7a1171694f06" }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 5. 2024년 수능 – 확통29\n", "\n", "## A. 문제이해\n", "\n", "![](https://github.com/guebin/PP2024/blob/main/imgs/2024-수능-확통-29.png?raw=true)\n", "\n", "## B. 슈도알고리즘\n", "\n", "`1`. 아래의 경우를 모두 나열한다.\n", "\n", "``` python\n", "a,b,c,d = 1,1,1,1 \n", "a,b,c,d = 1,1,1,2 \n", "....\n", "a,b,c,d = 6,6,6,6\n", "```\n", "\n", "`2`. 조건 $a \\leq c \\leq d$ 와 $b \\leq c \\leq d$ 를 동시에 만족하는\n", "경우를 센다.\n", "\n", "## C. 예비학습\n", "\n", "`# 개념1` – for문과 튜플" ], "id": "b9b3f985-e1e1-4a4e-b476-d4ff0b632bba" }, { "cell_type": "code", "execution_count": 176, "metadata": {}, "outputs": [], "source": [ "lst = [['최규빈',43052,'M'],['아이유',54321,'F'],['하니',11223,'F']]\n", "lst[0]" ], "id": "4fbef83c-0ca7-4a7e-861e-6041259bc0e3" }, { "cell_type": "markdown", "metadata": {}, "source": [ "아래의 코드가 가능하다." ], "id": "efd811de-a905-43d9-af10-69109b5e9a09" }, { "cell_type": "code", "execution_count": 187, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "['최규빈', 43052, 'M']\n", "['아이유', 54321, 'F']\n", "['하니', 11223, 'F']" ] } ], "source": [ "for [name,student_id,sex] in lst:\n", " print([name,student_id,sex])\n", "# [name,student_id,sex] = ['최규빈', 43052, 'M']\n", "# print([name,student_id,sex])\n", "# [name,student_id,sex] = ['아이유', 54321, 'F']\n", "# print([name,student_id,sex])\n", "# [name,student_id,sex] = ['하니', 11223, 'F']\n", "# print([name,student_id,sex])" ], "id": "4e406ae1-9f0d-4137-bbae-3496df517513" }, { "cell_type": "markdown", "metadata": {}, "source": [ "리스트를 튜플로 바꾼다면?" ], "id": "c38f4daa-9fb6-4a22-a218-894fbfa64f49" }, { "cell_type": "code", "execution_count": 203, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "('최규빈', 43052, 'M')\n", "('아이유', 54321, 'F')\n", "('하니', 11223, 'F')" ] } ], "source": [ "for (name,student_id,sex) in lst:\n", " print((name,student_id,sex))\n", "# (name,student_id,sex) = ['최규빈', 43052, 'M']\n", "# print((name,student_id,sex))\n", "# (name,student_id,sex) = ['아이유', 54321, 'F']\n", "# print((name,student_id,sex))\n", "# (name,student_id,sex) = ['하니', 11223, 'F']\n", "# print((name,student_id,sex))" ], "id": "553bd70e-307f-4efe-af13-a6ac3cc0d647" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`for (name,student_id,sex) in lst:` 대신에\n", "`for name,student_id,sex in lst:` 도 가능" ], "id": "283fdbff-2284-4a17-8483-97ed8b951890" }, { "cell_type": "code", "execution_count": 206, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "('최규빈', 43052, 'M')\n", "('아이유', 54321, 'F')\n", "('하니', 11223, 'F')" ] } ], "source": [ "for name,student_id,sex in lst:\n", " print((name,student_id,sex))\n", "# name,student_id,sex = ['최규빈', 43052, 'M']\n", "# print((name,student_id,sex))\n", "# name,student_id,sex = ['아이유', 54321, 'F']\n", "# print((name,student_id,sex))\n", "# name,student_id,sex = ['하니', 11223, 'F']\n", "# print((name,student_id,sex))" ], "id": "6bd0f367-9345-49f9-838e-2fe307dfb850" }, { "cell_type": "markdown", "metadata": {}, "source": [ "아래와 같이 컴프리헨션으로 만들 수도 있음." ], "id": "001e07d0-148b-4bc9-b996-e3c33c0c505d" }, { "cell_type": "code", "execution_count": 209, "metadata": {}, "outputs": [], "source": [ "[[name,student_id,sex] for name,student_id,sex in lst]" ], "id": "183813e2-0c1c-4445-9d79-182bbe77aec9" }, { "cell_type": "code", "execution_count": 210, "metadata": {}, "outputs": [], "source": [ "[(name,student_id,sex) for name,student_id,sex in lst]" ], "id": "f9a0bbb9-9a98-4b18-863e-5370bbf8a5f9" }, { "cell_type": "markdown", "metadata": {}, "source": [ "아래는 불가능" ], "id": "203733f9-88c1-4408-bbb5-deef293d558a" }, { "cell_type": "code", "execution_count": 213, "metadata": {}, "outputs": [], "source": [ "[name,student_id,sex for name,student_id,sex in lst]" ], "id": "b43ffd33-0c6a-4f7b-a970-9c9b24e9b3f0" }, { "cell_type": "markdown", "metadata": {}, "source": [ "하지만 괄호를 명확하게 쓰기만 하면 만들어짐" ], "id": "1114ce09-2462-4c36-9d2d-055efa81b6c3" }, { "cell_type": "code", "execution_count": 215, "metadata": {}, "outputs": [], "source": [ "[(name,sex) for name,student_id,sex in lst]" ], "id": "e2dbc01f-39ce-45ee-9302-eed31cfe2fae" }, { "cell_type": "code", "execution_count": 216, "metadata": {}, "outputs": [], "source": [ "[(sex,name) for name,student_id,sex in lst]" ], "id": "e2b8d702-b025-498a-8ae9-194c6f21b7c5" }, { "cell_type": "code", "execution_count": 218, "metadata": {}, "outputs": [], "source": [ "[name for name,student_id,sex in lst]" ], "id": "02456bba-5704-41f2-a05b-631abb8059dc" }, { "cell_type": "markdown", "metadata": {}, "source": [ "언더스코어(`_`)를 사용할 수도 있음." ], "id": "90c0e1ce-87be-428f-af6b-ae24ae6fa613" }, { "cell_type": "code", "execution_count": 220, "metadata": {}, "outputs": [], "source": [ "[name for name,_,_ in lst]" ], "id": "38e07469-96d3-4e99-902f-63f8654dd79b" }, { "cell_type": "markdown", "metadata": {}, "source": [ "언더스코어(`_`)를 사용시 `lst[0]`, `lst[1]` 등의 원소숫자와 일치하도록\n", "사용해야함" ], "id": "966c3e94-29ff-4464-836c-cc309ba1cb06" }, { "cell_type": "code", "execution_count": 226, "metadata": {}, "outputs": [], "source": [ "[name for name,_ in lst] # 이건 또 실행불가능.." ], "id": "ab53d343-4e5c-4fb3-aeff-88a7f918ac0d" }, { "cell_type": "code", "execution_count": 232, "metadata": {}, "outputs": [], "source": [ "[name for name, *args in lst] # 이건 실행가능" ], "id": "1bd92fae-066a-4730-80ea-b499e9d112e5" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`# 개념2` – 조건문\n", "\n", "아래의 리스트 원소 $l$ 중에서 조건 $1 < l \\leq 5$를 만족하는 원소는 모두\n", "몇개인가?" ], "id": "79f45e01-cb88-468a-bb0a-56df445eff72" }, { "cell_type": "code", "execution_count": 234, "metadata": {}, "outputs": [], "source": [ "lst = [1,2,3,4,5,6,7,8,9] \n", "lst" ], "id": "836ac4fb-d722-4538-9d23-d5319cdc7e74" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(풀이)" ], "id": "0750cc0e-b8a8-42df-ac14-2f03572919e1" }, { "cell_type": "code", "execution_count": 242, "metadata": {}, "outputs": [], "source": [ "sum([1