{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 04wk-1: 파이썬의 자료형 (6)\n",
"\n",
"최규빈 \n",
"2023-03-27\n",
"\n",
"
\n",
"\n",
"# 강의영상\n",
"\n",
"> youtube:\n",
"> \n",
"\n",
"- 강의 마지막에 집합 설명하려다가 다 못했습니다. 집합은 다음시간에\n",
" 다시 설명하겠습니다. 위 영상에서 집합설명한 부분은 무시하셔도\n",
" 됩니다.\n",
"\n",
"# 잡기술 (하지만 유용해)\n",
"\n",
"## 인덱싱고급 (스트라이딩)\n",
"\n",
"`-` 스트라이딩 \\[start:end:step\\]"
],
"id": "0de23d23-6b70-4237-9816-c4b679bec831"
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"lst = list('abcdefghijk')\n",
"lst"
],
"id": "6f66f483-073b-4959-bbee-cae80508032b"
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"lst[0:9:3]"
],
"id": "31c66313-1071-4c02-97fc-8e16125d4754"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 생략"
],
"id": "ed19b8cd-9b6b-4c21-9ec9-6d3dd4c6c742"
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"lst[0:9]\n",
"#lst[0:9:]\n",
"#lst[0:9:1]"
],
"id": "1282add6-7f77-4043-8dab-b9fdef1f31d8"
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"lst[0::3]"
],
"id": "00abac6f-5737-42ec-b4cc-8fac1634d17b"
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"lst[:8:3]"
],
"id": "932de1be-412c-4593-9395-54882e26de5e"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 예제1: 짝수/홀수 원소 추출\n",
"\n",
"아래와 같은 문자열이 있다고 하자."
],
"id": "b67964a3-cfb7-414d-b16c-f832251a0092"
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"lst = list('abcdefghijk')\n",
"lst"
],
"id": "e6423d6d-8591-4711-b630-5896f17f551d"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`index = 0,2,4, ...` 에 해당하는 원소를 출력하라."
],
"id": "1fdcd062-26b2-4559-9622-6044d06a85ae"
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"lst[::2]"
],
"id": "a7c02fad-4ebb-41a3-892e-6f1659d4543c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`index = 1,4,7 ...` 에 해당하는 원소를 출력하라."
],
"id": "5c58e803-e6e6-4f62-a87d-9e44d0250e4f"
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"lst[1::3]"
],
"id": "c39f7a9f-56a8-4c2c-8564-d9ebb532fb80"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 예제2: 세로로..\n",
"\n",
"------------------------------------------------------------------------\n",
"\n",
"(예제2를 위한 예비학습) 문자열에서 `\\n`을 출력하면 출력시 줄바꿈이\n",
"일어난다."
],
"id": "36423a29-fcef-452d-93c5-a3752319f535"
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1행\n",
"2행\n",
"3행"
]
}
],
"source": [
"print('1행\\n2행\\n3행')"
],
"id": "1b56e06c-3dbe-4e21-853b-030fe70ed4bf"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"예비학습 끝\n",
"\n",
"------------------------------------------------------------------------\n",
"\n",
"아래와 같은 문자열이 있다고 하자."
],
"id": "e78c7a37-36bd-4316-907b-a79e84084214"
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"너같이사랑스럽고\n",
"또예쁘고도멋지고\n",
"속훤히보이는너알\n",
"았어그동안고마웠\n",
"지정말정말사랑해"
]
}
],
"source": [
"txt = '너같이사랑스럽고\\n또예쁘고도멋지고\\n속훤히보이는너알\\n았어그동안고마웠\\n지정말정말사랑해'\n",
"print(txt)"
],
"id": "416388ec-d774-4b6a-b691-28c3dd7247db"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"위 문자열을 세로로 읽는 코드를 작성하라. (9칸씩 점프하면서 읽으면 된다)\n",
"\n",
"(풀이)"
],
"id": "85d933ef-2ca4-44ff-b697-598e37d51e09"
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"txt[::9]"
],
"id": "42474fec-89ac-4e79-aad6-e080a6b6d92d"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` step = -1 이면?"
],
"id": "fc23d5ba-a994-4d10-9803-33e1afa03097"
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"lst"
],
"id": "fe961f89-2f64-41cf-b298-3242580f5f73"
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"lst[::-1]"
],
"id": "3a3fbfd8-0c24-4656-966c-62502d264364"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 스트라이딩으로 `step = -1` 옵션 주기 vs 리스트의 `.reverse()` 메소드\n",
"이용하기\n",
"\n",
"관찰1: reverse 메소드는 리스트 자체를 변화시킴"
],
"id": "b9636dac-051f-430f-b074-1784f50f436e"
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"lst = list('abcdefgh')\n",
"lst"
],
"id": "feeae8ec-3e40-4ee4-82ef-090bd7b680ba"
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"lst.reverse() \n",
"lst"
],
"id": "1eb17164-63ae-4044-8c29-7b5f9a2c5c25"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"관찰2: \\[::-1\\]는 리스트는 변화시키지 않음"
],
"id": "37d4c6e0-7398-49cb-bbb4-407f6054ac1f"
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
"lst = list('abcdefgh')\n",
"lst"
],
"id": "bf2db592-8cbf-443b-92c9-c56ffd250b81"
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"lst[::-1]"
],
"id": "ee2994b6-8aca-44aa-bb8f-c6b7798762b4"
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"lst"
],
"id": "50e38e5d-d9ad-4d2a-8feb-524f0e305028"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` -step은 쓰기 까다롭다.\n",
"\n",
"(예제) 처음과 끝을 생략하지 않고 아래와 동일한 효과를 주는 코드를 만들어\n",
"보자."
],
"id": "3911e2cc-f156-49e7-a713-10a1601e0228"
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
"lst = list('abcdefgh')\n",
"lst[::-1]"
],
"id": "1d204c2e-4ffa-459b-86b1-a744d00eb7b3"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)\n",
"\n",
"결국 lst\\[?:?:-1\\]의 꼴에서 적당히 ?의 값을 채우면 된다. –\\> 어려워"
],
"id": "15e8ac7e-37bf-4df2-8dd2-7b7d991fea6a"
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [],
"source": [
"lst"
],
"id": "e1f61cec-6e26-4ab8-8bba-4c389c3ee6e4"
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
"lst[::-1]"
],
"id": "3ee6f2e7-2c94-4c00-b1bd-7acec2fb280b"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| None | a | b | c | d | e | f | g | h | None |\n",
"|:----:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:----:|\n",
"| ? | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |\n",
"| -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 | ? |"
],
"id": "32e3617c-4447-49aa-84bd-edb3fb05bcfa"
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
"lst[-1:-9:-1] "
],
"id": "0cf6003a-ba67-43da-b912-2c8fd1cbff10"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 컴프리헨션 고급 (if문이 포함된 컴프리헨션)\n",
"\n",
"`-` 예제: 제곱수중에서 12로 나누어 떨어지는 수만 원소로 가지는 리스트를\n",
"만들고 싶다.\n",
"\n",
"- 제곱수: 1,4,9,16,25,36, …\n",
"- 12로 나누어 떨어지는 수: 36, …\n",
"\n",
"(예비학습1)"
],
"id": "7611f742-619d-4892-a993-5a0117a0b21f"
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"12 % 4 # %는 나머지를 계산하는 연산자, 12를 4로 나누면 나머지가 0"
],
"id": "0cd083fe-695a-46c7-ac09-8cee4ed0d423"
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [],
"source": [
"12 % 5 # %는 나머지를 계산하는 연산자, 12를 5로 나누면 나머지가 2"
],
"id": "d20b8285-6f86-4020-a54b-7f6db24af1e4"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(예비학습2)"
],
"id": "f879e76f-e287-4c6f-9661-d6e747105c6e"
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [],
"source": [
"a = 2 ## a에 2를 \"대입\" 하라. "
],
"id": "f8b5b030-e6fe-4508-adc8-fadb7ce51c48"
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
"a == 2 # a에 들어있는 값이 2인지 \"test\"하라."
],
"id": "02810c1f-709c-4b58-bfcf-8385a46d496a"
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
"a == 3 # a에 들어있는 값이 3인지 \"test\"하라."
],
"id": "17cbabea-e111-4aa7-a2b2-061715ed13ab"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(예비학습3) if문"
],
"id": "d6ea3bc1-6b79-4bc9-b9e4-c4d3b74cfd35"
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [],
"source": [
"a= 3 \n",
"if a%2 == 0: \n",
" a_is='even' ## a%2==0 이 true일 경우만 실행된다. \n",
"else:\n",
" a_is='odd' ## a%2==0 이 false일 경우만 실행된다. "
],
"id": "27e767a0-c4bc-4061-a9a6-aaa06adfac31"
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [],
"source": [
"a,a_is"
],
"id": "1fe6d5d5-7e30-4141-89d5-45be43301bca"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이1) - 비어있는 리스트를 만들고 $\\to$ for문 + if문"
],
"id": "80aff259-243c-41c9-a3c8-995fdaedb6d9"
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [],
"source": [
"lst = list()\n",
"for i in range(1,101): \n",
" if i**2 % 12 == 0:\n",
" lst.append(i**2)"
],
"id": "078d5028-cc01-4e40-9b4e-8ccfb4f17690"
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [],
"source": [
"lst"
],
"id": "a39a799c-b176-404c-a990-ed86f574649c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이2) - if문이 포함된 리스트컴프리헨션"
],
"id": "94c1e979-8332-4247-b001-8964c61042ab"
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [],
"source": [
"[i**2 for i in range(1,101) if i**2 % 12 == 0]"
],
"id": "43e8da70-0080-4772-a943-6dbf37c803ac"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 함수고급 (if문이 포함된 리턴)\n",
"\n",
"`-` 홀수/짝수를 판별하는 함수 만들기 1"
],
"id": "659a1882-dbe7-4cee-ba19-e78665eff81f"
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [],
"source": [
"def test(x): \n",
" if x % 2 == 0: \n",
" return 'even'\n",
" else:\n",
" return 'odd'"
],
"id": "0aad1117-28b2-4bae-ae22-7871244dd0bb"
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [],
"source": [
"test(5)"
],
"id": "604cef52-01b3-4480-b582-a44a0be391c3"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(사용)"
],
"id": "5dd76e3d-696c-4e3e-a403-97fc995b2cbf"
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [],
"source": [
"[test(l) for l in list(range(1,11))]"
],
"id": "9893a55f-8a8a-49f4-8d74-bba3b00fbe51"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 홀수/짝수를 판별하는 함수 만들기 2"
],
"id": "ff09e05b-1277-41c3-957b-0e69820a4cd9"
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [],
"source": [
"def test(x):\n",
" return 'even' if x % 2 == 0 else 'odd'"
],
"id": "21ce4498-b25a-4c1f-8daa-417e5547bdcf"
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [],
"source": [
"test(4)"
],
"id": "c4944d4a-fb18-4ecd-a72f-725545b71ad2"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(사용)"
],
"id": "fecea8cb-020f-4b8b-9a89-afd0cc1bf7bb"
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [],
"source": [
"[test(l) for l in list(range(1,11))]"
],
"id": "a364a600-4a70-46f3-8361-7251fa5810af"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## len함수\n",
"\n",
"`-` 0차원 자료형은 len함수가 동작하지 않음"
],
"id": "3c1ce6cf-0d87-4fed-93e3-1457aea52fc9"
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [],
"source": [
"a=1 \n",
"len(a)"
],
"id": "37dcd50b-dfe8-4e80-803a-d4dc4c7e3a08"
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [],
"source": [
"a=True\n",
"len(a)"
],
"id": "ac8b387e-35ad-4965-bcee-679373d0f2d4"
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [],
"source": [
"a=3.14\n",
"len(a)"
],
"id": "f6c9b60f-9031-4b18-b6a0-ff186ee3cb24"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> note: 이것이 어떠한 수학적인 의미를 가지거나 0차원의 본질적진리를\n",
"> 뜻하는 것은 안미. R에서는 1,3.14,TRUE의 길이가 1로 존재함.\n",
"\n",
"`-` 1차원 자료형은 len함수가 동작"
],
"id": "bf9a656f-1bee-4f8a-ae08-ddddc8af645a"
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [],
"source": [
"a='guebin'\n",
"len(a)"
],
"id": "0cee34f5-d925-4fc2-8ac2-def074888f14"
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [],
"source": [
"a=[1,2,3,4,5,6]\n",
"len(a)"
],
"id": "9b7df96d-d507-4857-a75b-c929eaf1fa2f"
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [],
"source": [
"a=1,2,3,4,5,6 \n",
"len(a)"
],
"id": "996f033d-4cd6-4b48-b7cd-5cf0ac7d1a29"
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [],
"source": [
"a=range(10)\n",
"len(a)"
],
"id": "56b45323-fbe3-47e4-8dea-c6dc37bd4b5e"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 길이가 1인 1차원 자료형과 0차원 자료형은 다른것임"
],
"id": "8da3fc1c-3dd3-4ae5-ad2b-213bf5393cc4"
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [],
"source": [
"a='g'\n",
"len(a)"
],
"id": "8772a8aa-bd77-4464-9846-c1756ca9e0c1"
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [],
"source": [
"a=[1]\n",
"len(a)"
],
"id": "3719ee30-7814-47f8-bcd9-802652652dfc"
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [],
"source": [
"a=(1,)\n",
"len(a)"
],
"id": "bf629e62-3004-41dd-899b-950006607904"
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [],
"source": [
"a=range(1)\n",
"len(a)"
],
"id": "50f89f33-3b2e-4396-9134-3cea1338089b"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 길이가 0인 1차원 자료형도 존재함"
],
"id": "2eec737e-ce3f-4948-ba79-f6b0f4bfea37"
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [],
"source": [
"a=''\n",
"len(a)"
],
"id": "0b9bc122-20cb-437f-a1e6-ae10557a0a6e"
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [],
"source": [
"a=[]\n",
"len(a)"
],
"id": "f60ad92b-eb2b-4a12-a01a-751f1948c232"
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
"a=()\n",
"len(a)"
],
"id": "5fc6844f-2612-42d3-aa74-391725c1348e"
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {},
"outputs": [],
"source": [
"a=range(0)\n",
"len(a)"
],
"id": "ee8b6fab-29e9-412e-b533-f2e661bc059c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 딕셔너리 기본내용\n",
"\n",
"## intro: str, list, tuple 정리\n",
"\n",
"`-` str, list, tuple은 모두 시퀀스형이라는 공통점이 있다. $\\to$ 원소의\n",
"위치번호로 인덱싱이 가능"
],
"id": "9c6acaf8-21b1-46e9-a6b4-910b603b9a28"
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [],
"source": [
"lst = [1,2,3,4]"
],
"id": "c2f2ae0d-0c70-4fee-95da-95270f56dd98"
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [],
"source": [
"lst[0] # 위치번호=0"
],
"id": "3b18708d-6a10-48a9-9ed7-f800ec986463"
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {},
"outputs": [],
"source": [
"lst[-1] # 위치번호=-1"
],
"id": "85a5e116-6a92-400e-ac0f-cf3990807ef2"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` str, list, tuple은 차이점도 존재함. 잠깐 정리해보자.\n",
"\n",
"***시퀀스형의 카테고리***\n",
"\n",
"- 컨테니어형: list, tuple\n",
"- 균일형: str\n",
"- 가변형: list\n",
"- 불변형: tuple, str\n",
"\n",
"***표로 정리하면***\n",
"\n",
"| | 컨테니어형 | 균일형 |\n",
"|:------:|:----------:|:------:|\n",
"| 가변형 | list | . |\n",
"| 불변형 | tuple | str |\n",
"\n",
"`-` 시퀀스형이 아닌 1차원 자료형도 있을까? 원소의 위치번호로 인덱싱이\n",
"불가능한 자료형\n",
"\n",
"`-` 왜 이런게 필요할까?\n",
"\n",
"- 벡터에서 원소를 뽑는것은 정보의 모임에서 정보를 검색하는 것과 같다.\n",
"- 정보를 `순서`대로 나열한뒤에 그 `순서`를 이용하여 검색하는 방법은\n",
" 유용하다.\n",
"- 하지만 경우에 따라서는 `키워드`를 기억해서 그 `키워드`를 바탕으로\n",
" 정보에 접근하는 방법이 유용할 수 있다.\n",
"\n",
"***카카오톡 대화내용검색***\n",
"\n",
"(상황1) `오늘아침`에 와이프가 `뭔가`를 카톡으로 부탁했었음. 그런데 그\n",
"`뭔가`가 기억안남.\n",
"\n",
"(상황2) `개강전에` 동료교수와 함께 `저녁약속`을 카톡으로 잡았었음.\n",
"그런데 그게 언제인지 기억안남.\n",
"\n",
"(상황3) `오늘아침` 동료교수와 함께 `점심약속`을 카톡으로 잡았었음.\n",
"그런데 그 장소가 기억나지 않음.\n",
"\n",
"`-` 순서대로 정리된 자료를 검색할때는 시퀀스형이 유리하다. 그런데\n",
"키워드로 검색하고 싶을 경우는 딕셔너리 타입이 유리하다.\n",
"\n",
"## 선언\n",
"\n",
"`-` 방법1: 가장 일반적"
],
"id": "f040d982-9e06-473b-b5a8-161967e96ada"
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {},
"outputs": [],
"source": [
"dct = {'guebin':49, 'hanni':80}\n",
"dct"
],
"id": "07d9e78f-0754-4e47-957c-9589f9b29369"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 방법2: `dict()` 이용"
],
"id": "c6abb87a-c25e-43f6-9a53-dd039d6c3bd3"
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {},
"outputs": [],
"source": [
"dct = dict(guebin=49, hanni=80)\n",
"dct"
],
"id": "7109fa8e-683a-40f3-92c5-31184ec1e3ff"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 방법3: 중첩된 리스트를 만든 뒤에 형태변환"
],
"id": "cb753cc9-a8ed-4f5a-92f7-09e40e21beb5"
},
{
"cell_type": "code",
"execution_count": 113,
"metadata": {},
"outputs": [],
"source": [
"_lst = [['guebin',49],['hanni',80]]\n",
"_lst "
],
"id": "e0a92979-d594-435c-b24c-cfe4f731f47b"
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {},
"outputs": [],
"source": [
"dict(_lst)"
],
"id": "2fd19f0c-9023-43b7-a2ba-dc743166f82c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 방법4: 중첩된 튜플을 만든 뒤에 형태변환"
],
"id": "a60a5f08-2ad9-459c-8e64-d7ab094a553e"
},
{
"cell_type": "code",
"execution_count": 115,
"metadata": {},
"outputs": [],
"source": [
"_tpl = ('guebin',49), ('hanni',80)\n",
"_tpl"
],
"id": "5115710c-3a13-44e9-9edb-ae7d6c41670c"
},
{
"cell_type": "code",
"execution_count": 116,
"metadata": {},
"outputs": [],
"source": [
"dict(_tpl)"
],
"id": "897f2fa0-6c06-44e1-9ba1-9fce55d8d18e"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 원소추출\n",
"\n",
"`-` 원소의 위치로 추출할 수 없고, key로 추출해야 한다."
],
"id": "9810fedf-4ffd-43c5-b5a2-ef7a723227b6"
},
{
"cell_type": "code",
"execution_count": 117,
"metadata": {},
"outputs": [],
"source": [
"dct = {'guebin':49, 'hanni':80}\n",
"dct"
],
"id": "131e9f7d-1431-4124-ba16-b20305e223c2"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"guebin의 점수를 추출하고 싶다면?"
],
"id": "dde1f70b-a054-482a-a019-095352c1d7c2"
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {},
"outputs": [],
"source": [
"dct['guebin']"
],
"id": "5b9bfc63-6f94-4d3e-93c7-5d8a92d282e4"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 만약에 dict가 아니라 list로 정보를 저장했다면?\n",
"\n",
"(예제) 아래와 같은 리스트에서 guebin의 점수를 추출하고 싶다면?"
],
"id": "fe2b545c-4e06-4839-8cd6-cab376f9980d"
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {},
"outputs": [],
"source": [
"lst=[['guebin',49],['hanni',80]]\n",
"lst"
],
"id": "15fd189b-91d6-4ab2-b39d-5abe7465782e"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이1)"
],
"id": "64fa609d-6a0e-4ed9-a502-99aa5fcff5ff"
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {},
"outputs": [],
"source": [
"lst[0][1] # guebin의 점수를 출력하란 의미"
],
"id": "c284eb0b-9bc0-475e-b049-ec328ecf593b"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이2) – 진짜 최악"
],
"id": "9788844a-0797-480b-8418-1f8122a457ae"
},
{
"cell_type": "code",
"execution_count": 129,
"metadata": {},
"outputs": [],
"source": [
"[lst[i][1] for i in range(len(lst)) if lst[i][0] == 'guebin']"
],
"id": "f3a65032-c089-4185-8915-ab02ec1665cd"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이3) – 덜 최악"
],
"id": "868d97ca-aa2f-4649-938b-f89efc76abd2"
},
{
"cell_type": "code",
"execution_count": 132,
"metadata": {},
"outputs": [],
"source": [
"[score for name,score in lst if name == 'guebin']"
],
"id": "88ac58df-b9a3-4a6b-b839-1c7d3d432031"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` ’guebin’의 점수를 추출하는 코드 비교"
],
"id": "6497529b-b331-4cda-8a7b-835a1539f36a"
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"dct['guebin'] # 코드1: 단순하고, 가독성있음"
],
"id": "6fe5acce-5559-4283-be9e-13a8b35c10ed"
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"lst[0][1] # 코드2: 단순하지만, 가독성이 있는건 아님"
],
"id": "dd2e14dc-a65b-470c-bca5-41b3f516d320"
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"[lst[i][1] for i in range(len(lst)) if lst[i][0] =='guebin'] # 코드3: 단순하지도 않고, 가독성도 없음."
],
"id": "6bbd2d86-7a92-432c-986a-809511e29cf3"
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
"[score for name,score in lst if name=='guebin' ] # 코드4: 단순하지 않지만, 가독성은 있음"
],
"id": "af0118f0-862c-4628-9bbe-f3a51c12aa4b"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 원소추가, 변경, 삭제"
],
"id": "6fbe8393-dbbd-436e-a1f5-8f4bfa024e73"
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {},
"outputs": [],
"source": [
"dct={'guebin':49, 'hanni':80}\n",
"dct"
],
"id": "af6af135-877c-4942-924d-47dd02ded493"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 원소에 접근: guebin의 점수 출력"
],
"id": "6cb35d6c-3a9a-437d-9368-fa60c6277b2d"
},
{
"cell_type": "code",
"execution_count": 134,
"metadata": {},
"outputs": [],
"source": [
"dct['guebin']"
],
"id": "1daf01b2-a079-4b43-8a9a-97e19d9046c8"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 추가: hynn학생의 점수를 추가"
],
"id": "c8d5cc53-de9e-4dd1-8994-3703432d8f4b"
},
{
"cell_type": "code",
"execution_count": 136,
"metadata": {},
"outputs": [],
"source": [
"dct['hynn'] = 99"
],
"id": "c9a0132f-4bd6-42a7-8986-c4935f089fd1"
},
{
"cell_type": "code",
"execution_count": 137,
"metadata": {},
"outputs": [],
"source": [
"dct"
],
"id": "9e4eb12d-88df-4eeb-8204-bde73d714569"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 변경: hanni의 점수를 변경"
],
"id": "3cca1d10-e512-4b0a-812d-d4f6528e6237"
},
{
"cell_type": "code",
"execution_count": 139,
"metadata": {},
"outputs": [],
"source": [
"dct['hanni'] = 100 "
],
"id": "83ae32a6-ea36-471d-bee0-48aed2437dae"
},
{
"cell_type": "code",
"execution_count": 140,
"metadata": {},
"outputs": [],
"source": [
"dct"
],
"id": "e3bee8c1-c68d-4b9c-91b5-0aad0bded8f4"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 삭제\n",
"\n",
"(방법1)"
],
"id": "c06b410f-aab6-473d-a16c-adb6e45b9797"
},
{
"cell_type": "code",
"execution_count": 141,
"metadata": {},
"outputs": [],
"source": [
"dct={'guebin':49, 'hanni':80, 'hynn':99}\n",
"del dct['guebin'] \n",
"dct"
],
"id": "8376b5a5-87f7-4523-bcd1-b9b855d6b244"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(방법2)"
],
"id": "8d81932d-3add-49aa-9eb0-d4f9c05f1f69"
},
{
"cell_type": "code",
"execution_count": 142,
"metadata": {},
"outputs": [],
"source": [
"dct={'guebin':49, 'hanni':80, 'hynn':99} \n",
"dct.pop('guebin')"
],
"id": "77ca092f-54a9-4ac5-a767-eb2738cec7bb"
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [],
"source": [
"dct"
],
"id": "2ad146b4-e7b8-4846-820d-bab2ad2881da"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 참고로 리스트였다면 이러한 삭제작업역시 비효율적이었을 것임"
],
"id": "f341dfb6-821f-41c6-8029-2067939be696"
},
{
"cell_type": "code",
"execution_count": 143,
"metadata": {},
"outputs": [],
"source": [
"lst = [['guebin',49],['hanni',80],['hynn',99]] \n",
"lst"
],
"id": "d37fa8cd-f2dc-4406-a140-be6aa9bb8c16"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"guebin의 점수를 삭제하려면?"
],
"id": "f52d2780-3fee-4e4a-8811-00b3a017ae1e"
},
{
"cell_type": "code",
"execution_count": 150,
"metadata": {},
"outputs": [],
"source": [
"[[name,score] for name,score in lst if name != 'guebin']"
],
"id": "1f87b54e-93c8-4584-968f-c173fcfd3efe"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 연산\n",
"\n",
"`-` 하나있어요.."
],
"id": "e3647827-6957-409a-8994-f13fa4ef6b3e"
},
{
"cell_type": "code",
"execution_count": 159,
"metadata": {},
"outputs": [],
"source": [
"dct = {'guebin':49, 'hanni':80} \n",
"dct"
],
"id": "40dd3c89-2631-4cfd-ad6b-a7dd8e3ddd1d"
},
{
"cell_type": "code",
"execution_count": 160,
"metadata": {},
"outputs": [],
"source": [
"'guebin' in dct"
],
"id": "98b7ad04-0fea-42ac-9e8e-dda5c7de0b9a"
},
{
"cell_type": "code",
"execution_count": 161,
"metadata": {},
"outputs": [],
"source": [
"'hanni' in dct"
],
"id": "b25b9939-3c30-4551-9c01-62caf2a55f40"
},
{
"cell_type": "code",
"execution_count": 162,
"metadata": {},
"outputs": [],
"source": [
"'hynn' in dct"
],
"id": "06ce310d-a2cb-43db-80a7-7c65d8ab4e1c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` in은 사실 다른자료형도 가능했음\n",
"\n",
"(관찰1)"
],
"id": "150bb5c3-21c6-453c-a03f-69c69e96c1fb"
},
{
"cell_type": "code",
"execution_count": 152,
"metadata": {},
"outputs": [],
"source": [
"'a' in 'guebin' "
],
"id": "54c67497-9f1e-4a47-bc15-6b4c5c65baf3"
},
{
"cell_type": "code",
"execution_count": 153,
"metadata": {},
"outputs": [],
"source": [
"'b' in 'guebin' "
],
"id": "bb4b392b-76a6-4634-95b2-b4426cd17fd5"
},
{
"cell_type": "code",
"execution_count": 154,
"metadata": {},
"outputs": [],
"source": [
"'c' in 'guebin' "
],
"id": "6eb404d9-3f83-4974-8c8d-3fd8b5145b0f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(관찰2)"
],
"id": "d8c1d047-268e-44cc-8d3a-4899a0a8efcf"
},
{
"cell_type": "code",
"execution_count": 155,
"metadata": {},
"outputs": [],
"source": [
"tpl = 1,2,3 \n",
"tpl"
],
"id": "ba411feb-79b5-47b1-8af3-00c72415ec82"
},
{
"cell_type": "code",
"execution_count": 156,
"metadata": {},
"outputs": [],
"source": [
"1 in tpl"
],
"id": "0e07af8e-9464-45dd-9b9d-2eda4dae5949"
},
{
"cell_type": "code",
"execution_count": 157,
"metadata": {},
"outputs": [],
"source": [
"4 in tpl"
],
"id": "642fbd5e-7902-4802-a02b-381060d67b83"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(관찰3)"
],
"id": "82c708e4-5d9f-4551-a0df-879bf3501e50"
},
{
"cell_type": "code",
"execution_count": 135,
"metadata": {},
"outputs": [],
"source": [
"lst = [['guebin',49],['hanni',80],['hynn',99]] \n",
"lst"
],
"id": "46b35057-8ffe-4776-8393-9ed436e73fcf"
},
{
"cell_type": "code",
"execution_count": 158,
"metadata": {},
"outputs": [],
"source": [
"['guebin',49] in lst"
],
"id": "96841b96-7a45-4c0b-9cd0-979e4537bbe2"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` in연산자가 dict형에 사용되면 key를 기준으로 True, False를 판단한다.\n",
"\n",
"## 딕셔너리 특수기능\n",
"\n",
"**(pop)**"
],
"id": "930b950c-a5f4-43a7-9d4a-405012c6df8a"
},
{
"cell_type": "code",
"execution_count": 164,
"metadata": {},
"outputs": [],
"source": [
"dct = {'guebin':49, 'hanni':80} \n",
"dct.pop('hanni')\n",
"dct"
],
"id": "84f168c0-12c7-447e-b723-5960525bb119"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(get)**"
],
"id": "86a0fd84-686e-4f5b-8ff1-76e442a2239f"
},
{
"cell_type": "code",
"execution_count": 165,
"metadata": {},
"outputs": [],
"source": [
"dct = {'guebin':49, 'hanni':80} \n",
"dct"
],
"id": "2df6b4fb-5f44-481f-a079-abc508e83561"
},
{
"cell_type": "code",
"execution_count": 149,
"metadata": {},
"outputs": [],
"source": [
"dct.get('guebin') "
],
"id": "9a2455a0-9e75-493e-8f07-167a1069b1a2"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"아래와 같은 기능"
],
"id": "905390d0-46d4-4a50-9f9a-53b1c4f7f895"
},
{
"cell_type": "code",
"execution_count": 150,
"metadata": {},
"outputs": [],
"source": [
"dct['guebin']"
],
"id": "ee6634bd-0e0f-45cd-b619-713261eb6b22"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"미묘한 차이점이 존재함"
],
"id": "6401d346-5000-4cce-bc21-d0b370650643"
},
{
"cell_type": "code",
"execution_count": 151,
"metadata": {},
"outputs": [],
"source": [
"dct['hynn'] # hynn이 없어서 키에러 출력, 그런 key는 없다.. "
],
"id": "869b9ba1-6513-460f-b015-0e2e8a334f22"
},
{
"cell_type": "code",
"execution_count": 152,
"metadata": {},
"outputs": [],
"source": [
"dct.get('hynn') # hynn이 없으면 아무것도 출력안함 "
],
"id": "a5231d0e-fe8d-4a3e-b6fe-f2a343448805"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(keys,values,items)**\n",
"\n",
"`-` .keys()는 딕셔너리의 키를 리턴한다."
],
"id": "9d713168-d0ec-409d-99ad-daa5c72cb761"
},
{
"cell_type": "code",
"execution_count": 166,
"metadata": {},
"outputs": [],
"source": [
"dct = {'guebin':49, 'hanni':80} \n",
"dct"
],
"id": "65e989dc-348f-48f5-9128-2e3d239a2fd7"
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [],
"source": [
"_keys=dct.keys()\n",
"_keys"
],
"id": "c4395cc1-9ea0-442b-8081-d77e2d58f5b7"
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [],
"source": [
"type(_keys) # 리턴된 자료형은 이상한것임"
],
"id": "d706b078-b696-4a14-8ad9-36fa74f5dee8"
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [],
"source": [
"list(_keys) # 아무튼 그 이상한 자료형도 리스트화 가능 "
],
"id": "af1cd8e2-0cd9-41a3-bce6-82352efd8b16"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` .values()는 딕셔너리의 값들을 리턴한다."
],
"id": "1c9c3d38-2b70-4df4-adae-81bcde8180a8"
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [],
"source": [
"_values = dct.values()\n",
"_values "
],
"id": "400031b3-f7a0-4d68-b1e2-54abd1beb79a"
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [],
"source": [
"type(_values)"
],
"id": "3ab4c07b-ad42-4f1d-ae81-0a7581f3b894"
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [],
"source": [
"list(_values)"
],
"id": "2e966ef6-9f69-42db-9c1a-5558be0356f6"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` .items()는 딕셔너리의 (키,값)을 리턴한다."
],
"id": "6fae659e-60b7-4c03-ba49-686ee7a6d920"
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [],
"source": [
"_items = dct.items()\n",
"_items "
],
"id": "de23d6d7-36cc-4abd-a49d-02fe82c8f8e8"
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [],
"source": [
"type(_items)"
],
"id": "807955b6-259c-4874-84e7-c21985306ce8"
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [],
"source": [
"list(_items)"
],
"id": "b5627b39-598d-4b8e-b05a-efc667e00298"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## for문과 dict ($\\star$)"
],
"id": "8fee8c89-81cd-4d3e-a474-84e911846f8a"
},
{
"cell_type": "code",
"execution_count": 179,
"metadata": {},
"outputs": [],
"source": [
"dct = {'guebin': 49, 'hanni': 80}\n",
"dct"
],
"id": "a86cb042-6a3c-4b4d-a676-33b91191b888"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(예시1)"
],
"id": "4188a78c-64e1-4a49-992f-3c9afe226d29"
},
{
"cell_type": "code",
"execution_count": 182,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"guebin\n",
"hanni"
]
}
],
"source": [
"for k in dct.keys():\n",
" print(k)"
],
"id": "a128e5fa-23f2-466a-9e79-6c1d76c6aa02"
},
{
"cell_type": "code",
"execution_count": 183,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"guebin\n",
"hanni"
]
}
],
"source": [
"for k in dct:\n",
" print(k)"
],
"id": "43a3e3d6-1227-4c85-81ae-2616fe82c419"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 딕셔너리 그자체도 for문에 넣을 수 있다.\n",
"- k에는 value가 삭제되어 들어간다. (즉 key만)\n",
"- 결과를 보면 dct 대신에 dct.keys()와 list(dct)를 넣었을때와 결과가\n",
" 같다.\n",
"\n",
"> Note: list(dct) 하면 key만 리턴된다.\n",
"\n",
"(예시2)"
],
"id": "d7849c29-39f7-493f-a11a-db871a006cc4"
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"49\n",
"80"
]
}
],
"source": [
"for v in dct.values():\n",
" print(v)"
],
"id": "53aac535-3ec0-471d-9c81-64e5ad6b2933"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(예시3)"
],
"id": "6ceba978-4c13-4dd9-992e-8b21e56c71b7"
},
{
"cell_type": "code",
"execution_count": 187,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"('guebin', 49)\n",
"('hanni', 80)"
]
}
],
"source": [
"for i in dct.items():\n",
" print(i)"
],
"id": "2f215e97-3349-4942-9b1c-09cc1d01fd2f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(예시4)"
],
"id": "c23c0ced-db79-4a5d-a04d-b08c0a372fcc"
},
{
"cell_type": "code",
"execution_count": 193,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"guebin 49\n",
"hanni 80"
]
}
],
"source": [
"for k,v in dct.items():\n",
" print(k,v)"
],
"id": "6295eeda-c51d-4b07-af6f-38d03aed7ec4"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(예시5) – {}의 중간고사 점수는 {}점 입니다."
],
"id": "424232ab-109e-41eb-98c6-710fd97b8791"
},
{
"cell_type": "code",
"execution_count": 199,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"guebin의 중간고사 점수는 49점 입니다.\n",
"hanni의 중간고사 점수는 80점 입니다."
]
}
],
"source": [
"for name,score in dct.items():\n",
" print('{}의 중간고사 점수는 {}점 입니다.'.format(name,score))"
],
"id": "a106eb5c-e64b-477d-b18b-84d7c0dbc6a0"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 딕셔너리 고급내용 (1) ($\\star$)\n",
"\n",
"## dict에서 key혹은 value만 뽑아내기\n",
"\n",
"`-` 예제: 아래의 dict에서 key만 뽑아내고 싶다."
],
"id": "04b38f8e-3b88-47f6-ac01-9b24accde443"
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [],
"source": [
"dct = {'guebin':49, 'hanni':80} "
],
"id": "94df70b3-4777-48bf-80c6-2ef9633d8d2d"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이1)"
],
"id": "95f07fe9-a0ce-405b-b602-5dd60668c286"
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [],
"source": [
"list(dct)"
],
"id": "d416cc5a-ca74-46a4-89b8-e3fefbdfa986"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이2)"
],
"id": "1224b70b-7fc5-42dd-99ae-400568000b91"
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [],
"source": [
"list(dct.keys())"
],
"id": "fbf8bb94-37c8-4697-8b0c-2a4e313a8c8f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이3)"
],
"id": "7ac9e806-edbc-477e-8e7b-ed643947d07d"
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [],
"source": [
"[k for k in dct]"
],
"id": "f30e0c2d-c81f-4340-8c28-0ce2da653f95"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이4)"
],
"id": "9fa2710e-69f7-4b15-8432-f99ac64cb83f"
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {},
"outputs": [],
"source": [
"[k for k,v in dct.items()]"
],
"id": "cce2b078-5b12-4ede-82e2-c2987b79a6c0"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 예제: 아래의 dict에서 value만 뽑아내고 싶다."
],
"id": "6b362ec6-f05e-4b77-a2db-66ecf186e017"
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [],
"source": [
"dct = {'guebin':49, 'hanni':80} "
],
"id": "3d014038-46d1-4d0c-b2e9-0b556fcaaa57"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이1)"
],
"id": "c982dd75-f95d-4b0d-b478-e6ee70f931ec"
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [],
"source": [
"list(dct.values())"
],
"id": "b643d03f-fbdd-45e1-808b-2012b94c3706"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이2)"
],
"id": "3f33eea2-c7aa-4526-8009-1ce8d3579830"
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [],
"source": [
"[dct[k] for k in dct]"
],
"id": "6667a77c-da81-4474-9871-d1abfe96c918"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이3)"
],
"id": "90ed82e5-25e9-4a8f-914f-ae133c620c3b"
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {},
"outputs": [],
"source": [
"[v for v in dct.values()]"
],
"id": "93a8caa0-8330-4add-83bf-08cde4b9c196"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이4)"
],
"id": "1f71424f-4c4e-47a3-b7c6-7aa876d299fb"
},
{
"cell_type": "code",
"execution_count": 107,
"metadata": {},
"outputs": [],
"source": [
"[v for k,v in dct.items()]"
],
"id": "b4e85a31-ee2c-42e4-8c8e-7792289ee086"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 바꿔치기 (1)\n",
"\n",
"`-` 예제1: 아래와 같은 리스트가 있다고 하자."
],
"id": "7858ed69-37ba-4de4-aeab-4a3441ff907e"
},
{
"cell_type": "code",
"execution_count": 163,
"metadata": {},
"outputs": [],
"source": [
"lst = list('abcd'*2)\n",
"lst"
],
"id": "894332f3-262b-41d2-8860-df41e91190a0"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"아래의 규칙에 의하여 lst의 각 원소의 값을 바꾸고 싶다고 하자. 이를\n",
"구현하는 코드를 작성하라.\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",
"hint: 아래의 dct를 이용할 것"
],
"id": "69477a12-8dd6-48a3-b01d-aa0c5878f9bc"
},
{
"cell_type": "code",
"execution_count": 214,
"metadata": {},
"outputs": [],
"source": [
"lst = list('abcd'*2)\n",
"lst"
],
"id": "98123886-7c0b-4be7-a90b-52e052c4ac64"
},
{
"cell_type": "code",
"execution_count": 215,
"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": "2440de70-eb3d-459e-aeb8-fa0489f623e6"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "d82e5594-62ca-4072-a113-b0385e078d4a"
},
{
"cell_type": "code",
"execution_count": 224,
"metadata": {},
"outputs": [],
"source": [
"[dct[x] for x in lst]"
],
"id": "210013f7-3517-4a5b-aa3c-ad558073db30"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 예제2: 예제1을 역변환하라."
],
"id": "0e36ec15-3515-4583-9f6a-e0adf3159396"
},
{
"cell_type": "code",
"execution_count": 225,
"metadata": {},
"outputs": [],
"source": [
"dct = {'a':[1,0,0,0], 'b':[0,1,0,0], 'c':[0,0,1,0], 'd':[0,0,0,1]}"
],
"id": "c2aba97d-391a-41f0-a519-9f97c9f0934a"
},
{
"cell_type": "code",
"execution_count": 226,
"metadata": {},
"outputs": [],
"source": [
"lst= [[1, 0, 0, 0],\n",
" [0, 1, 0, 0],\n",
" [0, 0, 1, 0],\n",
" [0, 0, 0, 1],\n",
" [1, 0, 0, 0],\n",
" [0, 1, 0, 0],\n",
" [0, 0, 1, 0],\n",
" [0, 0, 0, 1]]\n",
"lst "
],
"id": "6f4fabcd-778d-4ad4-9899-f6124dca300c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "b630ab0b-c32d-4dd3-bad0-f8f24055e581"
},
{
"cell_type": "code",
"execution_count": 239,
"metadata": {},
"outputs": [],
"source": [
"[x for l in lst for x,y in dct.items() if y == l]"
],
"id": "5af336fa-32fb-4fcd-b005-7beee5583e00"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 바꿔치기 (2)\n",
"\n",
"`-` 예제1: 아래와 같은 리스트를 고려하자."
],
"id": "9390b478-3b90-4a70-b384-4ec01cb0dc7c"
},
{
"cell_type": "code",
"execution_count": 240,
"metadata": {},
"outputs": [],
"source": [
"lst = ['딸기','사과','바나나','딸기','사과','오토바이','자동차','버스','기차','오토바이','자동차']"
],
"id": "cd843f81-b422-4657-bc7e-646bbe3b4dc1"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"다음의 맵핑규칙에 따라서 위의 리스트의 원소를 바꾸어라.\n",
"\n",
"| 변환전 | 변환후 |\n",
"|:--------:|:------:|\n",
"| 딸기 | 과일 |\n",
"| 사과 | 과일 |\n",
"| 바나나 | 과일 |\n",
"| 오토바이 | 탈것 |\n",
"| 자동차 | 탈것 |\n",
"| 버스 | 탈것 |\n",
"| 기차 | 탈것 |\n",
"\n",
"(풀이)"
],
"id": "50fafdd9-e0f5-4785-9d96-c7cfde5119e6"
},
{
"cell_type": "code",
"execution_count": 242,
"metadata": {},
"outputs": [],
"source": [
"dct = {'과일':['딸기','사과','바나나'], '탈것':['오토바이','자동차','버스','기차']}\n",
"dct"
],
"id": "bf46f15b-15fe-4578-b257-101d6ab84c9b"
},
{
"cell_type": "code",
"execution_count": 246,
"metadata": {},
"outputs": [],
"source": [
"[x for l in lst for x,y in dct.items() if l in y]"
],
"id": "ff054db9-ab5a-427e-bf2d-f36e914ca3a9"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# HW 0327 (1)\n",
"\n",
"`ssh코드`: 1-2\n",
"\n",
"아래의 문자열을 고려하자."
],
"id": "93aab631-4460-4c6a-8b8d-e6bc9a2f2c55"
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"test_arr = 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSUGPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3Pbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XAt3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/EnmZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbxNrRFi9wrf+M7Q== schacon@mylaptop.local'"
],
"id": "9d8f9c5b-b4b1-4c1b-9126-f4b51354dfee"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`1`. 이 문자열에서 `index = 0,2,4,6,8,...` 에 해당하는 원소를 출력하는\n",
"코드를 작성하라. (2022-파이썬입문 중간고사 1-(2) 참고)"
],
"id": "15ff479c-3444-4225-bc8c-283899807d2f"
},
{
"cell_type": "code",
"execution_count": 140,
"metadata": {},
"outputs": [],
"source": [
"# 출력결과는 아래와 같아야 한다. "
],
"id": "8d62e13b-da3e-471d-9cc3-071f3ada7673"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "06c6546d-b6f2-45e2-8f20-7a2a1b8fa03a"
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"test_arr[::2]"
],
"id": "b381bac2-2c06-4109-916f-3604c9c1502b"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`2`. 위 문자열을 뒤집은 문자열을 구하는 코드를 구현하라.\n",
"(2022-파이썬입문 중간고사 1-(4) 참고)"
],
"id": "993f9230-146c-4859-b3bc-fafd7f4c21b4"
},
{
"cell_type": "code",
"execution_count": 141,
"metadata": {},
"outputs": [],
"source": [
"# 출력결과는 아래와 같아야 한다. "
],
"id": "d81051b6-1e63-42b2-97d0-62cf9b1f6c6f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "8f465ebd-389c-4ce0-bdfb-21e7c733afd7"
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"test_arr[::-1]"
],
"id": "a171fb2e-4358-4e8c-b876-2373054f33c4"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"------------------------------------------------------------------------\n",
"\n",
"`파이썬 프로그래밍 시험성적`: 3-4\n",
"\n",
"아래와 같은 dictionary가 있다고 가정하자."
],
"id": "4bec3777-3143-4e98-b214-efa3f5bd2108"
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"dct={'202212377': {'att': 65, 'rep': 45, 'mid': 0, 'fin': 10},\n",
" '202212473': {'att': 95, 'rep': 30, 'mid': 60, 'fin': 10},\n",
" '202212310': {'att': 65, 'rep': 85, 'mid': 15, 'fin': 20},\n",
" '202212460': {'att': 55, 'rep': 35, 'mid': 35, 'fin': 5},\n",
" '202212320': {'att': 80, 'rep': 60, 'mid': 55, 'fin': 70},\n",
" '202212329': {'att': 75, 'rep': 40, 'mid': 75, 'fin': 85},\n",
" '202212408': {'att': 65, 'rep': 70, 'mid': 60, 'fin': 75},\n",
" '202212319': {'att': 60, 'rep': 25, 'mid': 20, 'fin': 35},\n",
" '202212348': {'att': 95, 'rep': 55, 'mid': 65, 'fin': 90},\n",
" '202212306': {'att': 90, 'rep': 25, 'mid': 95, 'fin': 50},\n",
" '202212308': {'att': 55, 'rep': 45, 'mid': 75, 'fin': 30},\n",
" '202212366': {'att': 95, 'rep': 60, 'mid': 25, 'fin': 55},\n",
" '202212367': {'att': 95, 'rep': 35, 'mid': 0, 'fin': 25},\n",
" '202212461': {'att': 50, 'rep': 55, 'mid': 90, 'fin': 45}}"
],
"id": "6e826886-6566-4e6b-b494-b5777cdda092"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"여기에서 ‘202212377’ 등은 학번을, att는 출석점수, rep는 레포트점수,\n",
"mid는 중간고사 점수, fin은 기말고사 점수를 의미한다.\n",
"\n",
"`3`. 학생들의 학번을 아래와 같은 방식으로 출력하는 코드를 작성하라."
],
"id": "52a1c29f-9015-43ea-939b-2f7479648f2d"
},
{
"cell_type": "code",
"execution_count": 165,
"metadata": {},
"outputs": [],
"source": [
"# 출력예시"
],
"id": "8d755a80-6a40-4ad8-9bae-2e3fcb35031f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "88d879d1-1f69-433c-bb20-2d977b3f7f1f"
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"[k[:4]+'-'+k[4:] for k in dct]"
],
"id": "f4c58912-9060-49b7-a88d-7be4c3539bf7"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`4`. 출석점수가 70점 이상인(`>=70`) 학생들의 학번을 출력하는 코드를\n",
"작성하라. (2022-파이썬입문 중간고사 2-(3) 참고)"
],
"id": "0ccd42a3-c307-416a-b3a2-3312342d49b8"
},
{
"cell_type": "code",
"execution_count": 166,
"metadata": {},
"outputs": [],
"source": [
"# 출력예시 "
],
"id": "9d4094cd-861b-492d-b2cd-b98323ef4c4b"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "5049a4dd-d179-4413-bf36-a9924665faf4"
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"[k[:4]+'-'+k[4:] for k,v in dct.items() if v['att']>70]"
],
"id": "a53f0c4e-8c9b-4880-880e-543fc014c335"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"------------------------------------------------------------------------\n",
"\n",
"`Oxford-III`: 5–10 //\n",
"[reference](https://www.robots.ox.ac.uk/~vgg/data/pets/)\n",
"\n",
"아래는 이미지 파일명들이 저장된 string을 불러오는 코드이다."
],
"id": "04977363-361c-433c-a5dd-1de31c5cfb2b"
},
{
"cell_type": "code",
"execution_count": 11,
"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": "5d193377-5ff7-47be-94e6-772942c0acc4"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"txt의 출력 일부를 나타내면 아래와 같다.\n",
"\n",
"``` default\n",
"'Abyssinian_1.jpg\\nAbyssinian_10.jpg\\nAbyssinian_100.jpg\\nAbyssinian_100.mat\\nAbyssinian_101.jpg\\nAbyssinian_101.mat\\nAbyssinian_102.jpg\\nAbyssinian_102.mat\\nAbyssinian_103.jpg\\nAbyssinian_104.jpg\\nAbyssinian_105.jpg\\nAbyssinian_106.jpg\\nAbyssinian_107.jpg\\nAbyssinian_108.jpg\\nAbyssinian_109.jpg\\nAbyssinian_11.jpg\\nAbyssinian_110.jpg\\nAbyssinian_111.jpg\\nAbyssinian_112.jpg\\nAbyssinian_113.jpg\\nAbyssinian_114.jpg\\nAbyssinian_115.jpg\\nAbyssinian_116.jpg\\nAbyssinian_117.jpg\\nAbyssinian_118.jpg\\nAbyssinian_119.jpg\\nAbyssinian_12.jpg\\nAbyssinian_120.jpg\\nAbyssinian_121.jpg\\nAbyssinian_122.jpg\\nAbyssinian_123.jpg\\nAbyssinian_124.jpg\\nAbyssinian_125.jpg\\nAbyssinian_126.jpg\\nAbyssinian_127.jpg\\nAbyssinian_128.jpg\\nAbyssinian_129.jpg\\nAbyssinian_13.jpg\\nAbyssinian_130.jpg\\nAbyssinian_131.jpg\\nAbyssinian_132.jpg\\n ....... \n",
"```\n",
"\n",
"`5`. 각 파일명은 `\\n`으로 구분되어있다. 위의 스트링을 분해하여 아래와\n",
"같은 리스트를 생성하고 `fname_list`에 저장하라.\n",
"\n",
"``` python\n",
"# fname_list 의 출력결과는 아래와 같아야 한다. \n",
"['Abyssinian_1.jpg','Abyssinian_10.jpg', ... ,'yorkshire_terrier_98.jpg', 'yorkshire_terrier_99.jpg']\n",
"```\n",
"\n",
"(풀이)"
],
"id": "147eb617-d4f9-43f9-9b81-f4b23f2252ab"
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"fname_list = txt.split('\\n')\n",
"fname_list[:10]"
],
"id": "32bbfb59-002a-4b56-8d50-4d7bd60fdfdc"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`6`. 각 이미지파일명은 아래와 같은 규칙으로 저장되어 있다.\n",
"\n",
"- 파일명의 첫글자가 대문자이면 고양이를 의미하고 첫글자가 소문자이면\n",
" 강아지를 의미한다.\n",
"- `_` 이전의 영문명은 고양이 혹은 강아지의 종(breed)을 의미한다.\n",
"\n",
"이미지 파일명이 입력으로 오면 강아지인지 고양이인지 판단하여 ‘cat’ or\n",
"’dog’를 리턴하는 함수 `f`를 구현하라.\n",
"\n",
"(함수사용예시)"
],
"id": "13533b7f-d03e-413c-a001-1640fcf34c05"
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"f('yorkshire_terrier_99.jpg')"
],
"id": "f3aa8a8e-3488-409f-95d6-777a69905fe9"
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [],
"source": [
"f('Abyssinian_1.jpg')"
],
"id": "44bcb9a7-a9cc-43ce-ae02-11d14b8321b0"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "1795b3ad-3565-4993-b82c-8a1b664d1488"
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"def f(fname): \n",
" return 'cat' if fname[0].isupper() else 'dog' "
],
"id": "02210b2b-4946-472c-bf02-4ecd785d72cd"
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"f('yorkshire_terrier_99.jpg'), f('Abyssinian_1.jpg')"
],
"id": "8be3881a-a897-47c6-ad89-9048d27d87df"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`7`. 5의 결과로 나온 `fname_list`를 입력으로 하고 리스트의 각 원소가\n",
"고양이를 의미하는 그림인지 강아지를 의미하는 그림인지 나타내는 리스트를\n",
"만들어라.\n",
"\n",
"``` python\n",
"## 입력예시 \n",
"['Abyssinian_1.jpg','Abyssinian_10.jpg',...,'yorkshire_terrier_98.jpg', 'yorkshire_terrier_99.jpg']\n",
"\n",
"## 출력예시\n",
"['cat', 'cat', ... , 'dog', 'dog']\n",
"```\n",
"\n",
"(풀이)"
],
"id": "543fd9b2-16e9-4d52-8a69-14a82701fc9f"
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"result = [f(l) for l in fname_list]\n",
"result[:10]"
],
"id": "90ab8147-e4fe-4efb-b159-c54f9ea0282d"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`8`. 강아지 그림과 고양이 그림이 각각 몇 장씩 포함되어 있는지 파악하는\n",
"코드를 구현하라.\n",
"\n",
"(풀이)"
],
"id": "8b8c963f-3b8d-4949-8b6b-b2e6d1548409"
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"sum([s=='cat' for s in result]) # 고양이"
],
"id": "ea12b2f6-9eaf-4d16-a284-395bebf9abbb"
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"sum([s=='dog' for s in result]) # 강아지"
],
"id": "23f45df8-f436-458a-ad76-a02e6ae34519"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`9`. 포메라니안 종의 그림이 몇장있는지 파악하는 코드를 구현하라.\n",
"\n",
"**hint:** 포메라니안 그림은 파일명에 ‘pomeranian’ 을 포함한다.\n",
"\n",
"(풀이)"
],
"id": "37b1c47c-e9bd-4e6c-8d4f-9749df059fc3"
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"sum(['pomeranian' in s for s in fname_list])"
],
"id": "78c56b61-ad28-43dc-aa3b-241d26a8975a"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`10`. 아래의 `dct`는 포메라니안과 사모예드가 각각 몇장씩 포함되어있는지\n",
"정리하기 위해 임시로 정리한 dictionary이다. 각 종이 몇 장씩\n",
"포함되어있는지 구하여 `dct`를 수정하라."
],
"id": "93553662-32f8-4286-91fa-afbfcd20d9c3"
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"dct = {'pomeranian':0, 'samoyed':0} \n",
"dct"
],
"id": "98e4cf00-6497-4870-88ea-c51e6cfb7763"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"예를들어 포메라니안과 사모에예드의 그림이 각각 200장씩 있다면 아래와\n",
"같이 딕셔너리를 수정해야한다."
],
"id": "19da5f28-51af-4337-b3a1-92fcce59428b"
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [],
"source": [
"{'pomeranian':200, 'samoyed':200} "
],
"id": "c728e613-6036-4882-bb66-c99282866e4f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "08fb07f7-006f-4c3e-ba2f-4ac4d5d58847"
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"dct['pomeranian'] = sum(['pomeranian' in s for s in fname_list])\n",
"dct['samoyed'] = sum(['samoyed' in s for s in fname_list])\n",
"dct"
],
"id": "36d4eb4c-52ee-4fca-9734-15e4eca99056"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# HW 0327 (2)\n",
"\n",
"`1`. 아래와 같은 맵핑을 고려하자.\n",
"\n",
"| 문자 | 숫자 |\n",
"|:----:|:----:|\n",
"| a | 1 |\n",
"| b | 0 |\n",
"\n",
"이를 딕셔너리로 표현하면 아래와 같다."
],
"id": "0ff75d7a-306c-43e0-ae79-abcf72a64e7b"
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"dct = {'a':0, 'b':1} "
],
"id": "f9ba4ef5-d4e0-4feb-9205-023addb1b9d7"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"위 규칙에 따라서 아래의 리스트의 원소를 문자로 각각 변환하라."
],
"id": "52c15d2d-8197-403b-a9cd-e10c7e986e39"
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"lst = [1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1]"
],
"id": "6ed824de-46d0-4c0d-b10f-19607c9d663c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"``` python\n",
"# 출력은 아래와 같아야 한다. \n",
"['b', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'b', 'b', 'a', 'a', 'b', 'a', 'b']\n",
"```\n",
"\n",
"(풀이)"
],
"id": "07b351e4-7c2c-4935-8d6b-75e5b79ab564"
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"[k for l in lst for k,v in dct.items() if v==l]"
],
"id": "e01ea0b5-fa59-4254-866e-600ca9aa0341"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`2`. 아래와 같은 맵핑을 고려하자.\n",
"\n",
"| 월 | 의미 |\n",
"|:----------:|:--------:|\n",
"| 1,2 | 겨울방학 |\n",
"| 3,4,5,6 | 1학기 |\n",
"| 7,8 | 여름방학 |\n",
"| 9,10,11,12 | 2학기 |\n",
"\n",
"이러한 규칙에 맞게 아래의 리스트를 적절한 문자열로 변환하라."
],
"id": "3cbdc845-5c18-49fa-b046-f28a70351388"
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"month = [1,2,2,3,4,5,6,7,8,9,9,10,11,12] "
],
"id": "fcd4f0a4-62a2-4ef4-8ba2-377813310dfb"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"``` python\n",
"## 출력은 아래와 같아야 한다. \n",
"['겨울방학', '겨울방학', '겨울방학', '1학기', '1학기', '1학기', '1학기', '여름방학', '여름방학', '2학기', '2학기', '2학기', '2학기', '2학기']\n",
"```\n",
"\n",
"(풀이)"
],
"id": "392aa07f-d1e9-4d9d-8907-3f72b309baba"
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"dct = {'겨울방학':[1,2], '1학기':[3,4,5,6], '여름방학':[7,8], '2학기':[9,10,11,12]} \n",
"[k for m in month for k,v in dct.items() if m in v] "
],
"id": "bc36b4cc-7912-4e08-b587-8ee10587cf57"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`합성변환` 3-5.\n",
"\n",
"아래와 같은 맵핑을 고려하자.\n",
"\n",
"(규칙1)\n",
"\n",
"| 문자 | 숫자 |\n",
"|:--------:|:----:|\n",
"| 바나나 | 0 |\n",
"| 사과 | 1 |\n",
"| 오토바이 | 2 |\n",
"| 자동차 | 3 |\n",
"| 자전거 | 4 |\n",
"\n",
"(규칙2)\n",
"\n",
"| 아이템 | 카테고리 |\n",
"|:--------:|:--------:|\n",
"| 바나나 | 과일 |\n",
"| 사과 | 과일 |\n",
"| 오토바이 | 탈것 |\n",
"| 자동차 | 탈것 |\n",
"| 자전거 | 탈것 |\n",
"\n",
"각각의 규칙을 나타내는 딕셔너리는 아래와 같이 선언되어있다고 하자."
],
"id": "2e8b9fa4-9711-4974-a7d2-5c349435a597"
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
"dct1 = {'바나나':0, '사과':1, '오토바이':2, '자동차':3, '자전거':4} \n",
"dct2 = {'과일':['바나나','사과'], '탈것':['오토바이','자동차','자전거']} "
],
"id": "60d9f5c6-f0d1-4ffa-8dc1-e3124947e64c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`3`. 규칙1를 이용하여 아래와 같은 리스트를 변환하는 함수를 구현하고 그\n",
"함수를 `f`라 선언하라.\n",
"\n",
"``` python\n",
"# 입력 \n",
"[0,1,0,1,4]\n",
"\n",
"# 출력 \n",
"['바나나', '사과', '바나나', '사과', '자전거']\n",
"```\n",
"\n",
"(사용예시)"
],
"id": "b7332c10-9fb4-4c89-9fcd-1bfbd652295d"
},
{
"cell_type": "code",
"execution_count": 202,
"metadata": {},
"outputs": [],
"source": [
"f([0,1,0,1,4])"
],
"id": "bc8521e4-fa35-431e-95f6-ffbbe341da05"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "77c4b5bd-979a-446c-9163-72c298bac04c"
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
"def f(lst):\n",
" return [k for l in lst for k,v in dct1.items() if v == l] "
],
"id": "9420cc14-d186-4a3b-bf5e-62fdbacf290e"
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
"f([0,1,0,1,4])"
],
"id": "7cd2c049-8037-4d3e-894f-935cfb6202ee"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`4`. 규칙2를 이용하여 아래와 같이 리스트를 변환하는 함수를 구현하고 그\n",
"함수를 `g`라고 선언하라.\n",
"\n",
"``` python\n",
"# 입력 \n",
"['바나나','바나나','바나나','자동차']\n",
"\n",
"# 출력 \n",
"['과일','과일','과일','탈것']\n",
"```\n",
"\n",
"(사용예시)"
],
"id": "e8d11b58-f094-4eec-af4d-cfb500c41fd7"
},
{
"cell_type": "code",
"execution_count": 201,
"metadata": {},
"outputs": [],
"source": [
"g(['바나나','바나나','바나나','자동차'])"
],
"id": "3756176e-d285-4923-b1ec-29846b9eb502"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "fb96d280-af73-4b57-9e59-7f8e4c30ea70"
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"dct2"
],
"id": "f0098c85-332e-4591-ba1f-846dc1e145b3"
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"def g(lst):\n",
" return [k for l in lst for k,v in dct2.items() if l in v] "
],
"id": "a3888dfa-b706-496e-bcb1-c37f4fa023f3"
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"g(['바나나','바나나','바나나','자동차'])"
],
"id": "e1a92106-9dd0-4f19-bc3e-badb8a7352c9"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`5`. 규칙1-2를 이용하여 아래와 같은 숫자로 이루어진 입력을 ‘과일’,\n",
"‘탈것’ 중 하나로 바꾸는 코드를 구현하라.\n",
"\n",
"``` python\n",
"# 입력 \n",
"[0,1,0,1,3,4,2,2,3,4,1,0]\n",
"\n",
"# 출력 \n",
"['과일', '과일', '과일', '과일', '탈것', '탈것', '탈것', '탈것', '탈것', '탈것', '과일', '과일']\n",
"```\n",
"\n",
"**hint** $g(f(x))$ 를 이용하라.\n",
"\n",
"(풀이)"
],
"id": "a3afef7a-cdf7-45b2-99d2-9d334cef1f91"
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
"g(f([0,1,0,1,3,4,2,2,3,4,1,0]))"
],
"id": "912290e6-57a3-447b-9319-ba92d73905bc"
}
],
"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"
}
}
}