{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 04wk-2: 파이썬의 자료형 (7)\n", "\n", "최규빈 \n", "2023-03-29\n", "\n", "\n", "\n", "# 강의영상\n", "\n", "> youtube:\n", "> \n", "\n", "# 딕셔너리 고급내용 (2)\n", "\n", "## key의 조건\n", "\n", "`-` 조건1: 키로 쓸 수 있는 자료형은 정해져 있다.\n", "\n", "- int O, float O, bool O, str O, list X, tuple O, dict X, set X\n", "\n", "(예시1) dict의 키로 int를 사용" ], "id": "acacb058-b555-4cf4-9650-cbb6ccabff2c" }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "dct = {0:[1,2,3], 1:[2,3,4]} \n", "dct[0] # 인덱싱 하는거 같네?" ], "id": "07fc7019-7f39-44a5-a326-f5aa54808314" }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [], "source": [ "dct[-1] # 속았지?" ], "id": "832d01b9-d466-43ee-94a8-32b11bbcb57f" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(예시2) dict의 키로 float을 사용 \\<– 이렇게 쓰는 사람 본적이 없어요" ], "id": "90e62210-da5a-4e2a-b67e-1f093806d925" }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [], "source": [ "dct = {3.14:'π', 2.178:'e'}\n", "dct[3.14]" ], "id": "caaa96b9-b5e7-49e1-819a-f0f7f57b820d" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(예시3) dict의 키로 bool을 사용" ], "id": "3f4695b6-5ad8-489e-8cf7-9ecc39fcb785" }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [], "source": [ "dct = {True: '참이다', False: '거짓이다.'} \n", "dct" ], "id": "d14479fb-ec5c-40ed-a913-446085d727e9" }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [], "source": [ "dct[1<2]" ], "id": "dadb71ea-3da4-4850-9293-ccc28d757312" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(예시4) dict의 키로 str을 사용 ($\\star$)" ], "id": "a7aea244-3a77-4bde-9963-0c093f549507" }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "dct = {'guebin':[10,20,30,30], 'hanni':[10,20,25,40]}\n", "dct['guebin']" ], "id": "36da0ee1-b35e-4923-952b-b7605bb5be25" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(예시5) dict의 키로 list를 사용 $\\Rightarrow$ 불가능" ], "id": "96f4fc30-db8e-4554-bc9e-9a94a2deebe5" }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "dct = {[10,20,30,40]: 'guebin', [10,20,25,40]: 'hanni'} \n", "dct" ], "id": "7b66c7c9-37d5-4885-9acf-f45f2e5c953f" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(예시6) dict의 키로 tuple 사용 ($\\star$)" ], "id": "60735ae6-5268-48ff-9eb7-c9d69bd0daf7" }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "dct = {(10,20,30,40): 'guebin', (10,20,25,40): 'hanni'} \n", "dct" ], "id": "0e6c7390-ba51-43dd-a38f-0f7ef104b5aa" }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "dct[(10,20,30,40)]" ], "id": "8b2ea970-8ede-4b3f-96af-a5833ae72843" }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "dct[10,20,30,40]" ], "id": "096980a7-a7f3-44bf-8bee-83988e6e468c" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(예시7) dict의 키로 dict사용 $\\Rightarrow$ 불가능" ], "id": "e82cbbc6-933c-422c-93d3-959e3258a637" }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "dct = {{0:1}: 'guebin', {1:2}: 'hanni'} \n", "dct" ], "id": "26186683-861f-4866-a89f-2be4611d2dd8" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(예시8) dict의 키로 set사용 $\\Rightarrow$ 불가능" ], "id": "edeee0ed-f6e2-4aa4-af1d-1e8ea4327332" }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "dct = {{'샌드위치','딸기우유'}:'점심', {'불고기','된장찌개','김','콩자반'}: '저녁'}\n", "dct" ], "id": "8774cc76-2146-42ef-b6c5-4d2164244d06" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 조건2: 키는 중복해서 쓸 수 없다.\n", "\n", "(예시1)" ], "id": "cd6a253c-7246-447b-98ac-e0c05fa93a90" }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [], "source": [ "dct = {0:[1,2,3], 1:[2,3,4], 0:[3,4,5]} # 이렇게 쓰지 마세요\n", "dct " ], "id": "0fcf6ee8-c977-4a93-9b9c-a445c2a5bc7b" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## value의 조건\n", "\n", "`-` 없다… $\\Rightarrow$ dict는 컨테이너형!!\n", "\n", "## 딕셔너리 컴프리헨션\n", "\n", "`-` 예시1" ], "id": "ebfb2165-cc5f-4759-8646-de490e646f88" }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [], "source": [ "lst = [['딸기','사과'],['오토바이','자동차'],['컴퓨터','아이패드','마우스']]\n", "lst " ], "id": "5438e152-d4a7-4b29-854a-a8114ee4d7e7" }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "{i:lst[i] for i in range(3)}" ], "id": "0fdf2ced-847a-4803-ba0a-af8ab10577cb" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 예시2: key, val을 서로 바꾸는 예시" ], "id": "c9737c9e-c0bf-4dfe-9c37-14cf2b838f4f" }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [], "source": [ "dct = {'a':(1,0,0,0), 'b':(0,1,0,0), 'c':(0,0,1,0), 'd':(0,0,0,1)}\n", "dct" ], "id": "206a0969-8c5f-4e8f-94f7-939fe43449d3" }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [], "source": [ "{v:k for k,v in dct.items() }" ], "id": "ed864dbf-17fc-4312-882f-022b64e3d239" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 바꿔치기 (3)\n", "\n", "`-` 예제1: 아래와 같은 리스트가 있다고 하자." ], "id": "b0113468-8501-4c61-a44e-a15a2352e75c" }, { "cell_type": "code", "execution_count": 186, "metadata": {}, "outputs": [], "source": [ "lst = list('abcd'*2)\n", "lst" ], "id": "e033cca2-93cc-44d0-b9b9-94de523e3d50" }, { "cell_type": "markdown", "metadata": {}, "source": [ "아래의 규칙에 의하여 lst의 각 원소의 값을 바꾸고 싶다고 하자.\n", "\n", "| 변환전 | 변환후 |\n", "|:------:|:-----------:|\n", "| ‘a’ | \\[1,0,0,0\\] |\n", "| ‘b’ | \\[0,1,0,0\\] |\n", "| ‘c’ | \\[0,0,1,0\\] |\n", "| ‘d’ | \\[0,0,0,1\\] |\n", "\n", "이를 구현하는 코드를 작성하고, 역변환하는 코드를 작성하라.\n", "\n", "hint: 아래의 dct를 이용할 것" ], "id": "f4f59997-d767-48bd-9159-1cb2da0a2208" }, { "cell_type": "code", "execution_count": 187, "metadata": {}, "outputs": [], "source": [ "dct = {'a':[1,0,0,0], 'b':[0,1,0,0], 'c':[0,0,1,0], 'd':[0,0,0,1]}\n", "dct" ], "id": "2c2aebd8-0977-4a47-9e76-41e23411f356" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(풀이)\n", "\n", "변환하는 코드를 구현하면" ], "id": "8ccce46a-a600-463d-a45d-d630c5803a91" }, { "cell_type": "code", "execution_count": 188, "metadata": {}, "outputs": [], "source": [ "lst2= [dct[l] for l in lst] \n", "lst2" ], "id": "59cf5ec2-4450-486c-a29e-d483368a8662" }, { "cell_type": "markdown", "metadata": {}, "source": [ "역변환하는 코드를 구현하면\n", "\n", "(1단계)" ], "id": "54814a37-506c-4318-b885-a2ba7587ae43" }, { "cell_type": "code", "execution_count": 189, "metadata": {}, "outputs": [], "source": [ "dct_inv = {tuple(v):k for k,v in dct.items()}\n", "dct_inv" ], "id": "d4b38cf8-4ea8-4629-8e4b-89531de2a387" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(2단계)" ], "id": "7029227c-b136-436c-ba00-1ff290e9a8d6" }, { "cell_type": "code", "execution_count": 190, "metadata": {}, "outputs": [], "source": [ "[dct_inv[tuple(l)] for l in lst2]" ], "id": "fd2dc2b2-0c09-4e20-aab6-9f322cd5645f" }, { "cell_type": "markdown", "metadata": {}, "source": [ "***내생각***\n", "\n", "위와 같은 코드는 경우에 따라서 아래와 같은 복잡합 코드를 피할 수 있는\n", "장점이 있다." ], "id": "2ab212fc-e6a0-4338-a6d1-ad29a7a994f2" }, { "cell_type": "code", "execution_count": 191, "metadata": {}, "outputs": [], "source": [ "[x for l in lst2 for x,y in dct.items() if l==y]" ], "id": "8d92a82d-6fe0-4d15-b104-97483731498e" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 예제2: 아래와 같은 리스트가 있다고 하자. – 강의를 재촬영 했습니다." ], "id": "6a344af4-2d50-40f7-87e7-36951d0de872" }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "lst = ['딸기', '사과', '바나나', '바나나', '오토바이', '자동차', '기차']\n", "lst" ], "id": "e64393fc-fdea-432f-aa75-e63c9630d3d2" }, { "cell_type": "markdown", "metadata": {}, "source": [ "아래와 같은 규칙에 따라서 바꾸고 싶다고 하자.\n", "\n", "| 변환전 | 변환후 |\n", "|:--------:|:------:|\n", "| 딸기 | 과일 |\n", "| 사과 | 과일 |\n", "| 바나나 | 과일 |\n", "| 오토바이 | 탈것 |\n", "| 자동차 | 탈것 |\n", "| 버스 | 탈것 |\n", "| 기차 | 탈것 |\n", "\n", "(풀이1)" ], "id": "0921b09d-9f0c-4673-9023-626256f42c9e" }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "dct = {'딸기':'과일', '사과':'과일', '바나나':'과일', \n", " '오토바이':'탈것', '자동차':'탈것', '버스':'탈것', '기차':'탈것'}\n", "dct" ], "id": "a956fc6c-4558-4f94-96d0-ee2041d7531f" }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "[dct[l] for l in lst] " ], "id": "bd734a84-5e93-45be-9b10-527790afa6bd" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(풀이2) – 지난시간에 한 것" ], "id": "ce49501a-3b37-4719-9ee3-a64ac2d3dec0" }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "dct = {'과일':['딸기','사과','바나나'], '탈것':['오토바이','자동차', '버스', '기차']} \n", "dct" ], "id": "20128ef6-2c74-4ec8-b1be-bfc17072c69a" }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "[k for l in lst for k,v in dct.items() if l in v]" ], "id": "a9a540a3-ec2a-474c-9d4f-0789609a3c4d" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(풀이3)" ], "id": "28b94771-9021-4281-846c-0a10c4485f93" }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [], "source": [ "_dct = {l:k for k,v in dct.items() for l in v}\n", "_dct " ], "id": "c0ef59b8-cac0-402d-b5b8-3f6fbc585cb0" }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "[_dct[l] for l in lst]" ], "id": "378e4c90-09c6-47d8-8488-35cc115bf2d6" }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 집합 기본내용\n", "\n", "## 선언" ], "id": "e8f12ed0-3cc3-4cb4-b4bf-ac4eade78a3b" }, { "cell_type": "code", "execution_count": 250, "metadata": {}, "outputs": [], "source": [ "wishlist={'notebook','desktop'}\n", "wishlist" ], "id": "296bfc98-a4d2-421d-bff9-0279c19d5873" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 원소추출\n", "\n", "`-` 일단 인덱스로는 못합니다." ], "id": "1ac53df0-3a82-42cd-a14d-527d4b4e3612" }, { "cell_type": "code", "execution_count": 251, "metadata": {}, "outputs": [], "source": [ "wishlist={'notebook','desktop'}\n", "wishlist[0]" ], "id": "245a3afd-13d8-4104-8a22-b24eea7cef39" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 딱히 하는 방법이 없어요.. 그리고 이걸 하는 의미가 없어요.. (원소에\n", "접근해서 뭐하려고??)\n", "\n", "## 원소추가\n", "\n", "`-` 이건 의미가 있음" ], "id": "ac73e1c2-0d3f-4407-8a27-2df11ec847c1" }, { "cell_type": "code", "execution_count": 252, "metadata": {}, "outputs": [], "source": [ "wishlist={'notebook','desktop'} \n", "wishlist" ], "id": "b43609fe-0d40-464a-9696-50357d2b35ea" }, { "cell_type": "code", "execution_count": 253, "metadata": {}, "outputs": [], "source": [ "wishlist.add('ipad')\n", "wishlist" ], "id": "359a1f90-0776-45bc-ad43-9520fbecc179" }, { "cell_type": "code", "execution_count": 254, "metadata": {}, "outputs": [], "source": [ "wishlist.add('notebook') # 이미 원소로 있는건 추가되지 않음. \n", "wishlist" ], "id": "a8e27c56-d189-4504-8bd8-83e5199f8781" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 원소삭제" ], "id": "bd782a43-3b65-4702-8071-bf2e54001649" }, { "cell_type": "code", "execution_count": 203, "metadata": {}, "outputs": [], "source": [ "wishlist={'desktop', 'ipad', 'notebook'}\n", "wishlist" ], "id": "288e63e2-4748-4129-9be0-3c3fe7b8df82" }, { "cell_type": "code", "execution_count": 204, "metadata": {}, "outputs": [], "source": [ "wishlist.remove('notebook')" ], "id": "79ee609d-d5ea-4150-a0b7-8dc55e204163" }, { "cell_type": "code", "execution_count": 205, "metadata": {}, "outputs": [], "source": [ "wishlist" ], "id": "f40292c0-82b7-4139-8f60-1fc9eb484637" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 연산\n", "\n", "`-` in 연산자" ], "id": "c4b3819f-4a37-4a59-b85c-4db275ec0b8e" }, { "cell_type": "code", "execution_count": 206, "metadata": {}, "outputs": [], "source": [ "wishlist={'desktop', 'ipad', 'notebook'}\n", "wishlist" ], "id": "aef7d3b7-4583-4cdb-a22f-e6a25239ffc0" }, { "cell_type": "code", "execution_count": 207, "metadata": {}, "outputs": [], "source": [ "'notebook' in wishlist" ], "id": "a63a9416-0642-4e73-9dea-61e498759535" }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 참고로 `in`연산자는 집합에서만 쓰는것은 아님\n", "\n", "`-` 합집합, 교집합, 차집합" ], "id": "44ebbfc1-f718-4613-8252-fde8e85fef9a" }, { "cell_type": "code", "execution_count": 208, "metadata": {}, "outputs": [], "source": [ "day1 = {'notebook','desktop'}\n", "day2 = {'notebook','ipad'}" ], "id": "8581eec8-64ee-4060-b55e-4c1f84412132" }, { "cell_type": "code", "execution_count": 209, "metadata": {}, "outputs": [], "source": [ "day1 | day2 # 합집합" ], "id": "65368e25-335a-4ff6-b9ab-66724ad6decb" }, { "cell_type": "code", "execution_count": 210, "metadata": {}, "outputs": [], "source": [ "day1 & day2 # 교집합" ], "id": "651ff060-4259-444e-9b8c-f0caa8d24229" }, { "cell_type": "code", "execution_count": 211, "metadata": {}, "outputs": [], "source": [ "day1 - day2 # 차집합 " ], "id": "ee340b45-b2f1-44c7-96e0-f93263d298c2" }, { "cell_type": "code", "execution_count": 212, "metadata": {}, "outputs": [], "source": [ "day2 - day1 # 차집합" ], "id": "51ac2d2b-c905-4703-a6af-6563884bcb54" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 부분집합" ], "id": "ee0aea6e-a27e-4a0d-b29f-4659dfd7edda" }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [], "source": [ "day1 = {'notebook', 'desktop'}\n", "day2 = day1 | {'ipad'} " ], "id": "4e270836-73f6-41df-ab2b-780dbfe1b82b" }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [], "source": [ "day1 < day2 # day1는 day2의 부분집합인가? " ], "id": "66498d09-2ac8-4097-9530-58d797d57d0d" }, { "cell_type": "code", "execution_count": 215, "metadata": {}, "outputs": [], "source": [ "day2 < day1" ], "id": "d0a84e4b-413e-4507-ac75-3814beed2d3b" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 집합 특수기능\n", "\n", "`-` 합집합" ], "id": "079c1d7c-b88f-4163-8567-a7ea6b881537" }, { "cell_type": "code", "execution_count": 216, "metadata": {}, "outputs": [], "source": [ "day1 = {'notebook', 'desktop'}\n", "day2 = {'notebook','ipad'}" ], "id": "0f3fa968-a02a-43bf-b182-5d5f2f536337" }, { "cell_type": "code", "execution_count": 217, "metadata": {}, "outputs": [], "source": [ "day1.union(day2)" ], "id": "713fe8d0-ff08-4e4e-9cf2-4503b13c40aa" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 나머지 메소드는 스스로 찾아보세요\n", "\n", "## for문과 set" ], "id": "4b0b39d0-19ff-476a-b870-91392f7e862c" }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [], "source": [ "day1 = {'notebook', 'desktop'}\n", "day2 = {'notebook', 'ipad'}" ], "id": "ca05ef59-830e-4c02-96b6-c3dcb951c046" }, { "cell_type": "code", "execution_count": 219, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "notebook\n", "ipad\n", "desktop" ] } ], "source": [ "for i in day1|day2: \n", " print(i)" ], "id": "6d489f41-16d8-426f-badd-d235ec1d057a" }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 집합 고급내용\n", "\n", "## set 컴프리헨션\n", "\n", "`-` 예시1" ], "id": "f4c89023-e4fd-4b00-9958-a76c4fd4d6e9" }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [], "source": [ "lst = [1,2,1,1,3,4,5]\n", "{l for l in lst}" ], "id": "c799364a-dc24-40ca-b98c-19b7beb86b8f" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 유니크한 원소\n", "\n", "`-` 예제1: 아래의 list는 모두 몇 종류의 문자로 이루어져 있는가?" ], "id": "849ca42f-19bb-415f-a065-1ba2b878aa02" }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "outputs": [], "source": [ "lst=list('asdfasssdfdsasdfasdfasdfasdf')" ], "id": "5dd99ede-b627-416b-aa0d-08e439048478" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(풀이)" ], "id": "49e7b367-9d9e-4d73-bc73-934ce86c10b5" }, { "cell_type": "code", "execution_count": 166, "metadata": {}, "outputs": [], "source": [ "set(lst)" ], "id": "9da29aba-1a20-41e1-8d50-050ed31a5340" }, { "cell_type": "code", "execution_count": 167, "metadata": {}, "outputs": [], "source": [ "len(set(lst))" ], "id": "52e3b989-8556-4c58-aab8-cbfbd740d08a" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 예제2: 아래의 txt에서 어떠한 종류의 문자가 각각 몇번씩 사용되었는지\n", "빈도를 구하는 코드를 작성하라." ], "id": "5f2a842d-734b-425d-817d-65d7824d6100" }, { "cell_type": "code", "execution_count": 168, "metadata": {}, "outputs": [], "source": [ "txt = 'asdkflkjahsdlkjfhlaksglkjdhflkgjhlskdfjhglkajhsdlkfjhalsdkf'\n", "txt" ], "id": "8a05a2cd-48dc-4b0a-a4d6-6becb8b29ad7" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(풀이)" ], "id": "22691fe4-7657-44d1-8163-260d2144e841" }, { "cell_type": "code", "execution_count": 176, "metadata": {}, "outputs": [], "source": [ "{k:list(txt).count(k) for k in set(txt)}" ], "id": "39aadec2-290c-463a-9ca4-61bec56a702c" }, { "cell_type": "markdown", "metadata": {}, "source": [ "# HW\n", "\n", "`Oxford-III`: 1–5 //\n", "[reference](https://www.robots.ox.ac.uk/~vgg/data/pets/)\n", "\n", "아래는 이미지 파일명들이 저장된 string을 불러오는 코드이다." ], "id": "67873f6d-4432-4c64-999f-c64ad67ae910" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import requests\n", "url = 'https://raw.githubusercontent.com/guebin/PP2023/main/posts/01_PythonBasic/Oxford-IIIT.txt'\n", "txt = requests.get(url).content.decode()" ], "id": "66e04af4-d058-405b-b17f-8de50663e5fc" }, { "cell_type": "markdown", "metadata": {}, "source": [ "이미지파일이 저장된 형식은 아래와 같다.\n", "\n", " Abyssinian_1.jpg\n", " British_Shorthair_129.jpg\n", "\n", "**note**: `British_Shorthair`와 같이 종 이름 사이에 `_`가 들어있는\n", "경우도 있음.\n", "\n", "`1`. txt를 적당히 변환하여 아래와 같은 list를 만들어라." ], "id": "f3e1baba-2399-454b-bde2-509658f8fb1e" }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "lst[:10],lst[810:820]" ], "id": "8d37c4a8-46a4-4680-9844-edeadbc7d7e4" }, { "cell_type": "markdown", "metadata": {}, "source": [ "**hint1**" ], "id": "4cd912bf-7179-4ac7-905b-9462b7f9971c" }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [], "source": [ "'Abyssinian_1.jpg\\nAbyssinian_10.jpg'.split('\\n')" ], "id": "01c04340-a536-457b-a24b-0acc797669df" }, { "cell_type": "markdown", "metadata": {}, "source": [ "**hint2**" ], "id": "b828fd73-158f-4a2e-a8d0-14b592a01ed5" }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [], "source": [ "''.join(['British', 'Shorthair'])" ], "id": "dae41923-0fa1-44c3-a9d2-cd05437202f0" }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [], "source": [ "''.join(['Abyssinian'])" ], "id": "85a28842-e2a2-469c-8cb9-a89a56725ee5" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(풀이1)" ], "id": "9680ad91-5303-48e8-98b6-c1a5197e5714" }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [], "source": [ "lst = [''.join(filename.split('_')[:-1]) for filename in txt.split('\\n')]" ], "id": "2789b345-31fc-4806-99c9-7263ff8ee5b5" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(풀이2)" ], "id": "b67f23d9-99bc-4a96-a29a-fa35e91bb2bf" }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [], "source": [ "def f(filename): \n", " *name, _ = filename.split('_')\n", " return ''.join(name)\n", "lst = [f(filename) for filename in txt.split('\\n')]" ], "id": "60bb5253-c71b-40eb-b704-44e2f053f8db" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(확인)" ], "id": "a5cc0e76-4d52-4f80-8e5e-9b4c076b3c20" }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [], "source": [ "lst[:10],lst[810:820]" ], "id": "5484a0ad-3381-4ba8-87fb-8136154faa93" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`2`. 그림파일에는 총 몇가지 종류의 고양이와, 몇가지 종류의 강아지가\n", "있는가?\n", "\n", "**note:** 고양이사진은 대문자로 시작하고, 강아지 사진은 소문자로\n", "시작한다.\n", "\n", "**note**: 12종의 고양이, 25종의 강아지가 있음\n", "\n", "(풀이)" ], "id": "67cdafdf-f1e5-4800-9721-94b9733b7d4d" }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [], "source": [ "[s[0].isupper() for s in set(lst)].count(True) # 고양이 12" ], "id": "64092cf5-c207-4400-971d-2ab8faab8f9c" }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [], "source": [ "[s[0].isupper() for s in set(lst)].count(False) # 강아지 25 " ], "id": "a1e9d54d-e901-457d-90ce-7b4987258d5e" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`3`. 아래는 1번의 결과로 얻어진 lst의 첫 10개의 원소와 마지막 10개의\n", "원소이다." ], "id": "57fdc3fc-a66f-4a77-8724-cbe4d37278b3" }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [], "source": [ "lst[:10], lst[-10:]" ], "id": "ab9820fa-5f0a-4064-a450-265957eac306" }, { "cell_type": "markdown", "metadata": {}, "source": [ "적당한 변환을 정의하여 lst를 아래와 같이 바꾸어라." ], "id": "c84b4da1-66f1-447d-a1da-10b6d1f398f9" }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [], "source": [ "lst2[:10], lst2[-10:] # 바뀐 lst" ], "id": "103a78e8-275f-4cf2-8700-91169e287657" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(풀이1)" ], "id": "17aa5d4f-8863-4e81-9b67-01bcc4bcf0c7" }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [], "source": [ "dct = {'cat':[s for s in set(lst) if s[0].isupper()], 'dog': [s for s in set(lst) if not s[0].isupper()]}\n", "lst2 = [k for l in lst for k,v in dct.items() if l in v]" ], "id": "5331f7c6-2980-4245-a4ec-933198bea556" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(풀이2)" ], "id": "0956263e-e6e0-4b50-951d-8bc00b24e868" }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [], "source": [ "def f(fname): \n", " return 'cat' if fname[0].isupper() else 'dog'\n", "lst2= [f(fname) for fname in lst]" ], "id": "938dd910-a11c-430a-9428-009c3e6da340" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`4`. txt에는 강아지사진과 고양이사진이 모두 몇장씩 들어있는가?" ], "id": "7258b4e3-6083-476f-a84e-67044044f67f" }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [], "source": [ "## 출력예시 " ], "id": "4cb8fd3b-0c6f-4d68-880a-af3650cf8047" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(풀이)" ], "id": "0c6a08b8-dee6-48a1-b448-a8c9f3b88ecf" }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [], "source": [ "{k:lst2.count(k) for k in ['dog','cat']}" ], "id": "b7bdd388-68b8-4394-bc56-99554bd8afdb" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`5`. txt에 각 종별로 몇장의 사진이 있는지 조사하라." ], "id": "dcd898fa-23d9-454a-b251-e4b41c90898d" }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [], "source": [ "## 출력예시" ], "id": "603af7e0-a555-4e7d-b2b3-8c26f06e81f1" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(풀이)" ], "id": "84664499-bb65-49ab-9a91-2a88335a3bc0" }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [], "source": [ "{k:lst.count(k) for k in set(lst)}" ], "id": "124c1bac-7635-471f-93ee-0071acf6a3ac" } ], "nbformat": 4, "nbformat_minor": 5, "metadata": { "kernelspec": { "name": "python3", "display_name": "Python 3", "language": "python" }, "language_info": { "name": "python", "codemirror_mode": { "name": "ipython", "version": "3" }, "file_extension": ".py", "mimetype": "text/x-python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.16" } } }