{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 03wk-1: 파이썬의 자료형 (4)\n",
"\n",
"최규빈 \n",
"2023-03-20\n",
"\n",
"
\n",
"\n",
"# 강의영상\n",
"\n",
"> \n",
"\n",
"# list 고급내용 2\n",
"\n",
"## 리스트 원소 추가\n",
"\n",
"(예제) 비어있는 리스트를 만들고 원소 0,1,2를 차례로 추가하여 보자.\n",
"\n",
"(풀이1) `+` 연산이용"
],
"id": "08dcb81e-5298-4956-8cf5-cf39b60c242e"
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"a=[]\n",
"a"
],
"id": "43fc318d-b93a-4063-b483-1f97c0734c14"
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"a= a+[0]\n",
"a"
],
"id": "4eb299d3-e02f-4177-bd6b-6b74610b713d"
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"a= a+[1] # a = [0]+[1]\n",
"a"
],
"id": "5fc398a1-10e9-43e0-ab85-d6329f656a0c"
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"a= a+[2] # a = [0,1] + [2]\n",
"a"
],
"id": "4d371fcc-6ab9-4892-88d3-2365b7caf78f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이2) `+=` 이용"
],
"id": "439046a0-7d86-4f2e-a78c-dc4496f7c524"
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"a=[]\n",
"a+=[0]\n",
"a+=[1] \n",
"a+=[2] \n",
"a"
],
"id": "e1a1c6b8-0a64-4795-8046-20bbf37b039e"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 반복되는 문자를 제거하고 연산의 순서를 바꾼다.\n",
"\n",
"(풀이3) 리스트 특수기능 `.append()`를 이용"
],
"id": "18aa0377-bb38-4b5a-a656-f4556ccd7349"
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"a=[] "
],
"id": "07eee73f-f89c-47c2-aabd-62d77787be7f"
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
"a.append(0)\n",
"a.append(1)\n",
"a.append(2)\n",
"a"
],
"id": "b4e23d44-dfb0-4aaa-a3d8-0252319f6e67"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 아래는 불가능하다."
],
"id": "1faaa437-f188-4ad0-8ebd-3bb7758dec9d"
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
"a.append(0).append(1).append(2)"
],
"id": "1c495314-596a-427d-a792-0d622b101a68"
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [],
"source": [
"a.append(0,1,2)"
],
"id": "0acbd850-85c7-4a43-9063-1c757178c4f9"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## `a+[4]`와 `a.append(4)`의 차이점은?\n",
"\n",
"(관찰1)"
],
"id": "59e64684-1347-455f-819f-9892d15f315c"
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"a=[1,2,3]\n",
"a+[4] ## 리스트 a와 리스트 [4]의 연산결과를 알려줘 "
],
"id": "156c02f3-48b8-4ba2-9f7d-6576c4a6bba1"
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"a ## a는 그대로임. 변화없음 "
],
"id": "5d231af5-6b5f-457a-86f6-e287eb49f6d9"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(관찰2)"
],
"id": "d915441e-3cb4-440d-8b1a-91eb4eec0a72"
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"a=[1,2,3]\n",
"a.append(4)"
],
"id": "f6f94a68-6ff2-4f9b-af80-b54f79b1bf52"
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
"a ## a자체가 변화함 "
],
"id": "652e449e-f195-45d2-bb98-705f27178ce1"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"비슷해보이지만 굉장히 미묘한 차이가 있음\n",
"\n",
"`a.append(4)`: `a`에 4를 append하라 $\\to$ `a`가 변함\n",
"\n",
"`a+[4]`: `a`와 `[4]`를 연산하라\n",
"\n",
"## 리스트 특수기능\n",
"\n",
"**(append)**"
],
"id": "a48b35ef-0cab-4847-8dc6-7ff1dd48499a"
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [],
"source": [
"a=[1,2,3,4]\n",
"a.append?"
],
"id": "0bd3fb43-b8d6-43ad-8cef-ac7e5cd8d8b1"
},
{
"cell_type": "code",
"execution_count": 163,
"metadata": {},
"outputs": [],
"source": [
"a.append(5)\n",
"a"
],
"id": "e6ca368a-b585-40a7-ba53-a53abb95b295"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(clear)**"
],
"id": "833d2bdb-dc37-4e87-9e45-bc3894eec049"
},
{
"cell_type": "code",
"execution_count": 166,
"metadata": {},
"outputs": [],
"source": [
"a=[1,2,3,4]\n",
"a.clear?"
],
"id": "8e18a295-0987-4f6a-ba27-673e3c9912e6"
},
{
"cell_type": "code",
"execution_count": 168,
"metadata": {},
"outputs": [],
"source": [
"a.clear()\n",
"a"
],
"id": "0785ead4-51c0-4856-9d05-5c8ab61d1f6b"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(copy)**"
],
"id": "a8337273-31c7-409d-ad34-36f6fa1eb3f4"
},
{
"cell_type": "code",
"execution_count": 170,
"metadata": {},
"outputs": [],
"source": [
"a=[1,2,3,4]\n",
"a.copy?"
],
"id": "7146c588-741e-44f4-97ee-78252f4f8e41"
},
{
"cell_type": "code",
"execution_count": 171,
"metadata": {},
"outputs": [],
"source": [
"b=a.copy()\n",
"b"
],
"id": "8c587c54-cd32-4309-a77e-ad7f6d0928ea"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(count)**"
],
"id": "ff95f19a-a59c-48e4-a3c8-3eeb7728090d"
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"a=['a','a','b','b','b','c']\n",
"a.count?"
],
"id": "59d4197d-8905-42d8-a53f-2332729c4e9c"
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"a.count('a')"
],
"id": "aa0ae32e-2e35-4cbf-8e5e-0f09dfc51ac8"
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"a.count('b')"
],
"id": "2af35690-ee60-41b9-84ab-d9907a91f423"
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"a.count('c')"
],
"id": "ea4094a1-2087-4967-be63-44ad2e0f2960"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(extend)**"
],
"id": "aa9286d4-a836-447b-9bab-5a70f0007be2"
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"a=[1,2,3,4]\n",
"b=[-1,-2,-3,-4]"
],
"id": "610d3ef6-4ba6-40b1-97b3-2ccbf4493328"
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"a.extend(b)\n",
"a"
],
"id": "ae158036-6c06-429e-87c7-b153b7ced0db"
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"a=[1,2,3,4]\n",
"b=[-1,-2,-3,-4]"
],
"id": "054878f6-2774-41e6-8ae1-83e1b3cb36c7"
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"a.append(b)"
],
"id": "befbd3a3-828e-44a0-a37f-4daebd2a9ba1"
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"a"
],
"id": "c99fa162-719e-4885-99f1-5a42c271cb3d"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(index)**"
],
"id": "d8ce0b53-f140-43d9-bd16-6b6703fe3e23"
},
{
"cell_type": "code",
"execution_count": 127,
"metadata": {},
"outputs": [],
"source": [
"a=[11,22,'a',True, 22,'a']\n",
"a.index?"
],
"id": "81084029-0603-4c5e-b87c-b58e362592be"
},
{
"cell_type": "code",
"execution_count": 128,
"metadata": {},
"outputs": [],
"source": [
"a.index(11)"
],
"id": "5226cc1e-0113-44ce-8071-1b258e3fa226"
},
{
"cell_type": "code",
"execution_count": 129,
"metadata": {},
"outputs": [],
"source": [
"a.index(22)"
],
"id": "ceb8422f-a6d1-4477-8638-ff555432b24c"
},
{
"cell_type": "code",
"execution_count": 130,
"metadata": {},
"outputs": [],
"source": [
"a.index('a')"
],
"id": "9d21375b-b3bd-45ba-9853-f8abe8069f0d"
},
{
"cell_type": "code",
"execution_count": 131,
"metadata": {},
"outputs": [],
"source": [
"a.index(True)"
],
"id": "87de829c-0697-4780-81fe-e4ceff106fc2"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(insert)**"
],
"id": "15c064c3-6194-4a40-b03b-d2171bc85153"
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"a=[1,2,3]\n",
"a.insert?"
],
"id": "42ac5c44-8a25-4397-9bb7-f5593fddeee0"
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"a.insert(1,88) \n",
"a"
],
"id": "5ddb180a-258c-4f7c-b572-4a1c7586501f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(pop)**"
],
"id": "2a783c0e-8194-4cb1-a3f9-9f01b4f7ccf0"
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"a=['a',1,2,'d']\n",
"a.pop?"
],
"id": "e38a3df0-46ef-4fe3-9ff1-ac04f721d061"
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"a.pop() # index=-1 이므로 마지막원소가 나타남"
],
"id": "f8a4b902-c813-43ba-b5cd-f94a759d0ff1"
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"a # a는 마지막 원소가 사라진 상태"
],
"id": "3e3f5f28-3686-4dbb-bf8b-36f24ffd6c64"
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"a.pop(0) # index=0 이므로 첫번쨰 원소가 나타남"
],
"id": "7c66da37-052b-47a2-8d25-7b5deb4c5c9b"
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"a # a에는 첫번째 원소가 사라진 상태"
],
"id": "f803baf4-4a07-45a1-8698-c5abcf37da3a"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(remove)**"
],
"id": "f6967453-485f-4f14-a113-a0c94eb5d2e9"
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"a=['a',2,3,'d']\n",
"a.remove?"
],
"id": "d2b16621-dc4e-4729-bbbd-b196fcd3c84f"
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"a.remove('d')"
],
"id": "4fc769f9-6695-4b56-9f8e-052e741b7b00"
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"a"
],
"id": "515b51de-6e8f-48e5-a4d7-baec886407e4"
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"a.remove('a')"
],
"id": "c5fd20de-b3c4-4b09-9b12-eaefeea45acb"
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"a"
],
"id": "ec2e67aa-2eda-4007-873c-6931ea57f584"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(reverse)**"
],
"id": "6fcbee44-46ef-4155-ab22-436754f0e672"
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"a=[1,2,3,4]\n",
"a.reverse?"
],
"id": "ce005fac-7e25-4944-9c40-ada3094288d0"
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"a.reverse()\n",
"a"
],
"id": "d4ac7fb3-ee36-4748-ac04-a766ebd1ae45"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(sort)**"
],
"id": "8ed89a27-9650-4e81-a919-5d597d060e9f"
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [],
"source": [
"a=[1,3,2,4]\n",
"a.sort?"
],
"id": "1e244223-79ee-4a21-bbf5-8f066891d7f6"
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"a.sort()\n",
"a"
],
"id": "d6f31bb5-56fa-406b-a396-f22f6c9f9196"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(다른예제들)"
],
"id": "fc531724-5a78-4cdf-a4a1-e7f68e630ef5"
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"a=list('guebin')\n",
"a"
],
"id": "24bc1337-e44b-4836-9884-375fceb1814d"
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"a.sort()\n",
"a"
],
"id": "56b556fa-9cd5-48f0-a659-8aa06db5248d"
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"a.sort(reverse=True)\n",
"a"
],
"id": "150b530d-3b34-4c12-a5d6-3ce89bb9dfd9"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 중첩리스트\n",
"\n",
"`-` 리스트는 리스트를 원소로 받을 수 있으므로 아래와 같이 중첩된\n",
"리스트를 만들 수 있다."
],
"id": "496647cc-a25d-4ba3-a7b7-e1bb362d6c55"
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [],
"source": [
"A=[[1,2,3],\n",
" [4,5,6],\n",
" [7,8,9]]\n",
"A"
],
"id": "265050ae-8660-4c7f-8290-e54284a8458b"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` A는 1차원인 벡터가 아니라 2차원인 매트릭스로 이해할 수 있다.\n",
"구체적으로는 아래와 같은 매트릭스로 이해할 수 있다\n",
"\n",
"\\$"
],
"id": "d5fd25c4-16c7-4051-b7e7-f4b37da9dbbf"
},
{
"cell_type": "raw",
"metadata": {
"raw_mimetype": "tex"
},
"source": [
"\\begin{bmatrix}\n",
"1 & 2 & 3 \\\\ \n",
"4 & 5 & 6 \\\\ \n",
"7 & 8 & 9 \n",
"\\end{bmatrix}"
],
"id": "85d9385b-9f03-4005-993f-c0ac813e66ed"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\\$\n",
"\n",
"`-` A에서 (2,1)의 원소를 뽑고싶다 = 4를 뽑고싶다"
],
"id": "d54b3c43-4fa4-48ac-befe-9d5ec72d4765"
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [],
"source": [
"A[1,0] # R에서는 이게 가능했죠"
],
"id": "544df6fe-1560-44ef-8faa-00919c4dd632"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 실패"
],
"id": "9d315392-9f7a-4c3c-8d0f-ffb4d7c10d4f"
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [],
"source": [
"A[1][0]"
],
"id": "274ed111-130e-48c4-81a2-4942d64b056b"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 성공\n",
"\n",
"`-` 성공의 이유를 분석해보자."
],
"id": "df668264-4782-46e5-a589-48ed3a3752d6"
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"A"
],
"id": "36a0b588-58ce-4a5c-bab3-acc825de5e34"
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
"A[1]"
],
"id": "cafa6a58-77ef-46cb-b376-7b5e46a5eda1"
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
"A[1][0]"
],
"id": "e747ab73-a5d3-42ef-9ea0-430404b919c3"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 매트릭스는 아니지만 매트릭스 같음! - 1차원 배열을 다차원 배열로\n",
"확장할 수 있는 기본 아이디어를 제공함\n",
"\n",
"# list 고급내용 3\n",
"\n",
"`-` 리스트컴프리헨션을 다룬다.\n",
"\n",
"## 예비학습1: for문 벼락치기\n",
"\n",
"`-` 리스트 컴프리헨션을 이해하기 전에 for문에 대하여 알아보자.\n",
"\n",
"프로그램안에서 반복해서 무엇인가를 하고싶다 $\\to$ for"
],
"id": "4516f120-87a7-4f90-8326-6ae4348e6fb6"
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"0\n",
"1\n",
"2\n",
"3"
]
}
],
"source": [
"for i in [0,1,2,3]: ## 반복실행계획\n",
" print(i) ## 반복실행할내용, 탭을이용하여 들여쓰기해야한다. "
],
"id": "1437cc7a-b879-4165-88d7-0e85af24a434"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(예제) 1,2,3,4의 합을 for문을 이용하여 구해보자."
],
"id": "97ecd390-8eea-4dac-96f0-0edab6bad558"
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [],
"source": [
"_sum = 0 "
],
"id": "31f51722-9aa2-4be9-b4c9-5909e25a7a52"
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [],
"source": [
"_sum = 0\n",
"for i in [1,2,3,4]: \n",
" _sum = _sum + i "
],
"id": "106e9e1c-0448-42de-b726-93f3479a28b6"
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [],
"source": [
"_sum"
],
"id": "f6f814fc-0584-4470-b276-c19335da2313"
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {},
"outputs": [],
"source": [
"_sum = 0\n",
"i=1 \n",
"_sum = _sum + i ## 1 <= 0+1\n",
"i=2\n",
"_sum = _sum + i ## 3 <= 1+2 \n",
"i=3 \n",
"_sum = _sum + i ## 6 <= 3+3\n",
"i=4\n",
"_sum = _sum + i ## 10 <= 6+4 "
],
"id": "d773c10a-8978-4f09-a969-26bc9a184415"
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [],
"source": [
"_sum"
],
"id": "2152c3e9-06f3-4cc3-8ac4-60a6891a2719"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 궁금: 아래와 같은 코드가 있다고 하자.\n",
"\n",
"``` python\n",
"for i in ????: \n",
" print(i)\n",
"```\n",
"\n",
"??? 자리에 올 수 있는건 무엇일까?\n",
"\n",
"- 대답하기 어려움.\n",
"- 일단 list는 가능했음.\n",
"\n",
"(예시1)"
],
"id": "69a25d1a-1901-43a3-8607-84f6c60967f3"
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1\n",
"2\n",
"3\n",
"4"
]
}
],
"source": [
"for i in [1,2,3,4]: \n",
" print(i)"
],
"id": "80a8c05a-70fc-4457-b6f2-91e989a961ac"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(예시2)"
],
"id": "f2ace515-0ef0-4ef0-82dd-198231771d78"
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"a\n",
"b\n",
"c\n",
"d"
]
}
],
"source": [
"for i in ['a','b','c','d']: \n",
" print(i)"
],
"id": "bd88359f-24af-4b7a-aa90-3bcaa2cd112c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(예시3)"
],
"id": "8933f494-c34d-494e-9593-67d05f74ab6d"
},
{
"cell_type": "code",
"execution_count": 115,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"a\n",
"b\n",
"c\n",
"d"
]
}
],
"source": [
"for i in 'abcd': \n",
" print(i)"
],
"id": "565f3ce3-a771-4646-b535-2a7af5663439"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(예시4)"
],
"id": "a0e5a39d-8b22-448a-ac09-5abbef9f11fb"
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1"
]
}
],
"source": [
"for i in '1': \n",
" print(i)"
],
"id": "36ee3033-ca5a-42bd-84a3-82ce507308e0"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(예시5)"
],
"id": "2112e879-c1f4-4e4f-ae59-6d45da1d7545"
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {},
"outputs": [],
"source": [
"for i in 1: \n",
" print(i)"
],
"id": "1f5c742e-53d7-497a-bd6b-3710c2f25c03"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(예시6)"
],
"id": "58d901a7-dc17-48bf-9f60-5b5165ed519b"
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9"
]
}
],
"source": [
"for i in range(10): \n",
" print(i)"
],
"id": "08c8e8f2-34c1-463b-bf05-7a9b2ad21b8c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 예비학습2: range\n",
"\n",
"`-` `range(0,10)` 선언해보기"
],
"id": "610dd22c-b6db-4860-9377-81ecf05d3857"
},
{
"cell_type": "code",
"execution_count": 130,
"metadata": {},
"outputs": [],
"source": [
"range(0,10)"
],
"id": "10e105d1-4a76-47b7-985b-ad46a3b4045c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 이게뭐야?\n",
"\n",
"`-` 도움말 확인하기"
],
"id": "bd919b0f-3b6b-492f-bea8-95ef7e1dd8d7"
},
{
"cell_type": "code",
"execution_count": 135,
"metadata": {},
"outputs": [],
"source": [
"_tmp = range(0,10)\n",
"_tmp?"
],
"id": "6c00b01e-0300-4e5c-8745-9a8da0b0343a"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 우리가 아는 범위에서는 모르겠음.. 이런게 있나보다 하고 넘어가야\n",
" 하겠음\n",
"\n",
"`-` 형태변환으로 range(0,10)의 느낌 찾기"
],
"id": "9af0ce3f-f522-43d1-9d4a-42bc3fbe0ff7"
},
{
"cell_type": "code",
"execution_count": 448,
"metadata": {},
"outputs": [],
"source": [
"list(range(0,10)) # 0을 포함, 10을 미포함 "
],
"id": "aae8fcff-cfa1-4f0c-b66b-d436824d7b1a"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 이게 중요한것임. range(0,10)를 리스트화시키면 \\[0,1,…,9\\] 와 같은\n",
" 리스트를 얻을 수 있음. $\\Rightarrow$ range(0,10)은 \\[0,1,…,9\\] 와\n",
" “비슷한 것” 임\n",
"\n",
"`-` `range()`의 활용"
],
"id": "da1551b1-fdcd-4726-a70a-81fc84c11aab"
},
{
"cell_type": "code",
"execution_count": 453,
"metadata": {},
"outputs": [],
"source": [
"list(range(10)) # 0은 생략가능"
],
"id": "dd75341d-7407-4b80-8669-b186bee09f57"
},
{
"cell_type": "code",
"execution_count": 144,
"metadata": {},
"outputs": [],
"source": [
"list(range(2,10)) # 2는 포함, 10은 미포함 "
],
"id": "16c34e53-054a-470b-aaa3-ec32464bda89"
},
{
"cell_type": "code",
"execution_count": 148,
"metadata": {},
"outputs": [],
"source": [
"list(range(1,10,2)) # 2는 포함, 10은 미포함 "
],
"id": "2aca36e3-bc47-42dc-a2f0-dfb1bef931a9"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"------------------------------------------------------------------------\n",
"\n",
"## 리스트 컴프리헨션\n",
"\n",
"`-` 예제: $2^0, 2^1, 2^2, 2^3$를 원소로 가지는 리스트를 생성하라.\n",
"\n",
"(풀이1) 직접입력"
],
"id": "06884542-817d-4aa8-8ba6-0565932b5158"
},
{
"cell_type": "code",
"execution_count": 386,
"metadata": {},
"outputs": [],
"source": [
"x= [2**0, 2**1, 2**2, 2**3] \n",
"x"
],
"id": "aef6d733-758b-4b10-ab93-24d795addef5"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이2) for문을 이용함"
],
"id": "d90ac962-e470-48b1-90ce-6c6e5fa2c9b6"
},
{
"cell_type": "code",
"execution_count": 159,
"metadata": {},
"outputs": [],
"source": [
"x=[] \n",
"for i in [0,1,2,3]:\n",
" x.append(2**i) "
],
"id": "020c566f-8a87-4639-99c3-391c1b3fb7d5"
},
{
"cell_type": "code",
"execution_count": 160,
"metadata": {},
"outputs": [],
"source": [
"x"
],
"id": "43b01ced-0d88-47f1-96d7-fd06fac523f7"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이3) for문을 이용함"
],
"id": "59c56d5a-7c83-46c1-a48d-e6259d51b3c8"
},
{
"cell_type": "code",
"execution_count": 161,
"metadata": {},
"outputs": [],
"source": [
"x=[] \n",
"for i in [0,1,2,3]:\n",
" x = x+[2**i]"
],
"id": "82717b80-f843-448f-a137-02d0c5ab478f"
},
{
"cell_type": "code",
"execution_count": 162,
"metadata": {},
"outputs": [],
"source": [
"x"
],
"id": "38055b5c-9c40-4ef8-8e31-6c8860b58263"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이4) for문을 이용함"
],
"id": "ca02018d-78db-45d5-912c-72dbc3d431e4"
},
{
"cell_type": "code",
"execution_count": 163,
"metadata": {},
"outputs": [],
"source": [
"x=[] \n",
"for i in [0,1,2,3]:\n",
" x += [2**i]"
],
"id": "2d0d854d-802a-429d-a728-dec2e735e657"
},
{
"cell_type": "code",
"execution_count": 164,
"metadata": {},
"outputs": [],
"source": [
"x"
],
"id": "a4403cbf-4690-4cc4-a279-81150c2225ef"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이5) 리스트컴프리헨션을 이용한 풀이"
],
"id": "00c3d7ec-00ea-4504-a6ac-9df0800c9018"
},
{
"cell_type": "code",
"execution_count": 167,
"metadata": {},
"outputs": [],
"source": [
"[2**i for i in [0,1,2,3]]"
],
"id": "7d7be65b-ad17-404f-80fa-7f52cdb4e543"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 리스트컴프리헨션의 문법 암기방법\n",
"\n",
"- 집합에서 조건제시법을 연상\n",
"- $\\{2^0,2^1,2^2,2^3\\}=\\{2^i: i \\in \\{0,1,2,3\\} \\}$\n",
"\n",
"`-` 리스트컴프리헨션이란?\n",
"\n",
"- 리스트를 매우 효율적으로 만드는 테크닉\n",
"- for문에 비하여 가지고 있는 장점: (1) 코드가 간결하다 (2) 빠르다\n",
"\n",
"## 리스트 컴프리헨션 연습\n",
"\n",
"`-` 예제1: 리스트 컴프리헨션을 이용하여 아래와 같은 리스트를 만들어라."
],
"id": "f940fb44-385c-4625-91a6-9fbcdc3d5e10"
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [],
"source": [
"['SSSS','PPPP','AAAA','MMMM']"
],
"id": "3b6d56da-6073-42de-827c-fd5d77108f45"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "bf389c46-63d5-4e6f-a7bc-0fdc47ca8643"
},
{
"cell_type": "code",
"execution_count": 172,
"metadata": {},
"outputs": [],
"source": [
"[i*4 for i in 'SPAM']"
],
"id": "a22cda39-d4cf-4e2e-ba7b-cf487af491ad"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 예제2: 리스트컴프리헨션을 이용하여 아래와 같은 리스트를 만들어라."
],
"id": "9d6f416a-cc22-46e3-a432-a460f582ab76"
},
{
"cell_type": "code",
"execution_count": 401,
"metadata": {},
"outputs": [],
"source": [
"['X1','X2','X3','Y1','Y2','Y3']"
],
"id": "bc0929ff-1ee2-401e-b8ad-bf5c24199ba8"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "ce6a2e19-13ed-4bc7-9eb0-0a7e97e3a23e"
},
{
"cell_type": "code",
"execution_count": 179,
"metadata": {},
"outputs": [],
"source": [
"[i+j for i in 'XY' for j in '123']"
],
"id": "bf7de6eb-1bb4-45d1-b67b-3be3b43a3026"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 예제: 리스트컴프리헨션을 이용하여 아래와 같은 리스트를 만들어라."
],
"id": "471917a8-a161-49a6-a312-9389c1cef104"
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [],
"source": [
"['stat1', 'stat2', 'stat3', 'math1', 'math2', 'math3']"
],
"id": "9b782eaf-9afc-4d39-a238-816f72145e53"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "72a9e0f9-b859-42dc-a1e3-7bad60ba8a2e"
},
{
"cell_type": "code",
"execution_count": 182,
"metadata": {},
"outputs": [],
"source": [
"[i+j for i in ['stat', 'math'] for j in '123']"
],
"id": "ce34675d-b72c-4d78-b7d5-b351b165ba76"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(다른풀이) 참고로 for문을 쓰면 좀 복잡해진다."
],
"id": "f265f56d-3e28-466b-8398-6a3c2cefd4c2"
},
{
"cell_type": "code",
"execution_count": 183,
"metadata": {},
"outputs": [],
"source": [
"_lst = [] \n",
"for x in ['stat','math']: \n",
" for y in '123': \n",
" _lst = _lst + [x+y] "
],
"id": "f8a18591-8941-493e-9a85-9cf0ceff9d45"
},
{
"cell_type": "code",
"execution_count": 184,
"metadata": {},
"outputs": [],
"source": [
"_lst "
],
"id": "11d1f632-4436-4447-b764-fd89285c0529"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 예제: 리스트컴프리헨션과 문자열 `'jbnu'`를 이용하여 아래와 같은\n",
"리스트를 만들어라."
],
"id": "f29bf682-c766-4e41-8eae-50fd01a09f83"
},
{
"cell_type": "code",
"execution_count": 186,
"metadata": {},
"outputs": [],
"source": [
"['j','b','n','u']"
],
"id": "885c7344-13e0-4eed-a9f6-276192191f98"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(다른풀이) 아래와 같이 풀면 된다는것은 알고 있음"
],
"id": "d4cdc485-da5d-494f-843b-e4c38e07146d"
},
{
"cell_type": "code",
"execution_count": 187,
"metadata": {},
"outputs": [],
"source": [
"list('jbnu')"
],
"id": "3af7ccbe-b973-46fe-af9e-0bff2d40104c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "0f608393-4fed-4cff-8d3b-84cff7c607df"
},
{
"cell_type": "code",
"execution_count": 188,
"metadata": {},
"outputs": [],
"source": [
"[i for i in 'jbnu']"
],
"id": "0b2bd10f-b474-4fe0-830c-acdb5f035c4d"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 예제: 리스트컴프리헨션을 이용하여 아래와 같은 리스트를 만들어라."
],
"id": "17c42030-20aa-4644-9dea-db41e842e178"
},
{
"cell_type": "code",
"execution_count": 189,
"metadata": {},
"outputs": [],
"source": [
"['X1', 'X2', 'X3', 'X4', 'X5', 'X6', 'X7', 'X8', 'X9', 'X10', 'X11', 'X12']"
],
"id": "92e8da0a-1ff5-4112-9001-8b610cc73be3"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "62b15c73-c949-48eb-9c17-3660e5ea93db"
},
{
"cell_type": "code",
"execution_count": 244,
"metadata": {},
"outputs": [],
"source": [
"['X'+str(i) for i in range(1,13)]\n",
"#['X'+str(i) for i in list(range(1,13))]"
],
"id": "ea67c6f8-4339-4b4d-adc7-15042e16ddbf"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 리스트 컴프리헨션과 for문의 미묘한 차이\n",
"\n",
"(경우1)"
],
"id": "f1b65452-574e-49d0-affb-df8d3d698430"
},
{
"cell_type": "code",
"execution_count": 245,
"metadata": {},
"outputs": [],
"source": [
"x=777 \n",
"lst = [] \n",
"for x in 'jbnu': \n",
" lst = lst + [x]\n",
"lst "
],
"id": "7c72a93c-b191-4d69-b8cf-24826a9f29da"
},
{
"cell_type": "code",
"execution_count": 246,
"metadata": {},
"outputs": [],
"source": [
"x"
],
"id": "9d663b2f-05bc-4470-b190-a737ee3bdf8c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(경우2)"
],
"id": "1430d499-19bd-46e0-b3e8-208cb7068ce6"
},
{
"cell_type": "code",
"execution_count": 203,
"metadata": {},
"outputs": [],
"source": [
"x=777\n",
"lst = [x for x in 'jbnu'] \n",
"lst "
],
"id": "7ed1c924-ace4-45cd-a002-45c990af15a4"
},
{
"cell_type": "code",
"execution_count": 204,
"metadata": {},
"outputs": [],
"source": [
"x"
],
"id": "f297a504-1fc9-4d43-8c4e-e5e38fc0412a"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> 진짜 미묘하게 다르죠?\n",
"\n",
"# 튜플 기본내용\n",
"\n",
"## 리스트 vs 튜플\n",
"\n",
"`-` 컨테이너형타입이라는 점, 그리고 연산 및 인덱싱을 하는 방법은\n",
"리스트와 같음 - 차이점1: \\[\\] 대신에 ()를 사용한다. - 차이점2:\n",
"불변형이다. (원소의 값을 바꿀 수 없음) - 차이점3: 하나의 원소를 선언할\n",
"때는 (1,)와 같이 해야 한다. - 차이점4: 의미가 명확할때는 튜플의 ()를\n",
"생략가능하다.\n",
"\n",
"`-` 컨테이너형이라는 것이 무슨의미?"
],
"id": "41488cf0-9c1b-4548-8c2a-69a7df721aca"
},
{
"cell_type": "code",
"execution_count": 235,
"metadata": {},
"outputs": [],
"source": [
"a=(4,6,'pencil', 3.2+4.6j, [3,4]) "
],
"id": "807babae-60a4-40c4-a21a-033c0754b692"
},
{
"cell_type": "code",
"execution_count": 236,
"metadata": {},
"outputs": [],
"source": [
"type(a[2])"
],
"id": "c6b8e5f8-2533-4d63-b589-e9a6f18e9a02"
},
{
"cell_type": "code",
"execution_count": 237,
"metadata": {},
"outputs": [],
"source": [
"type(a[3])"
],
"id": "d222f112-6b09-4601-9eaa-0daeb1c796b2"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 불변형이라는 것은 무슨의미?"
],
"id": "09d1f5b3-32e9-412a-8407-d655375b9490"
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"a[2] = 'Pencil'"
],
"id": "dfc4e99f-a3dc-4b38-aeb0-1de44f5e9699"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"참고로 a를 튜플이 아니라 리스트로 선언하면 값이 잘 바뀐다."
],
"id": "18067e91-1988-4001-95b7-81fcb7187c7b"
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"a=[4,6,'pencil', 3.2+4.6j, [3,4]]"
],
"id": "ea44ddfa-e197-49c3-815b-bbc1f2c78ee5"
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"a[2]"
],
"id": "9b35e577-bd3f-454a-8d54-b9db0970c0f9"
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"a[2]='Pencil'"
],
"id": "6295e598-ad22-4a49-afbf-63f1718d93b4"
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"a"
],
"id": "13a0b85a-c020-4e89-886f-782488d6b724"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 하나의 원소로 이루어진 튜플을 만들때는 쉼표를 붙여야 함."
],
"id": "a5308931-5495-49a9-a0df-f3930e35ea11"
},
{
"cell_type": "code",
"execution_count": 238,
"metadata": {},
"outputs": [],
"source": [
"[1]+[2,3,4]"
],
"id": "deee1ecf-de50-4296-bf63-bcf8d4a817bc"
},
{
"cell_type": "code",
"execution_count": 239,
"metadata": {},
"outputs": [],
"source": [
"(1,)+(2,3,4)"
],
"id": "1c6099aa-1000-42fb-8f48-9de35781094f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 마지막차이점! 의미가 명확할때 튜플의 괄호는 생략가능하다. (이게\n",
"중요합니다)"
],
"id": "5a89ebec-16fc-4ee5-b6ac-172f261b274d"
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"a=1,2\n",
"a"
],
"id": "5eb95deb-5fd5-4143-ab0c-e6add5312062"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"의미가 명확할때 생략해야함"
],
"id": "9571ba6d-7db7-4cc7-8edf-8dcfbacd2273"
},
{
"cell_type": "code",
"execution_count": 240,
"metadata": {},
"outputs": [],
"source": [
"1,2 + 3,4,5 "
],
"id": "f7b3f544-a24b-4284-98d9-1a47d0de5c94"
},
{
"cell_type": "code",
"execution_count": 241,
"metadata": {},
"outputs": [],
"source": [
"(1,2) + (3,4,5) "
],
"id": "2ab0a46c-2260-4f90-95fc-f0f200f76126"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 선언\n",
"\n",
"`-` 소괄호를 이용"
],
"id": "d9f0b7f6-e89f-4bd7-ab3b-5104cbce54d1"
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"a=(1,2,3)\n",
"a"
],
"id": "2c5a5f04-930b-47d2-b284-15b906771a61"
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"type(a)"
],
"id": "849abfdc-8ffb-4ae8-98e5-2e49178ec075"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 생략가능하다는 점이 포인트"
],
"id": "f3e6cb2c-9b7d-4430-96b9-ee49bfaba7a5"
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"a=1,2,3\n",
"a"
],
"id": "a3ff19ea-d4da-4bb6-accf-c7c430f8c721"
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"type(a)"
],
"id": "62a29947-7c7e-4b3e-83de-ae60e4f5891c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 원소가 하나인 튜플을 만들고 싶다면?"
],
"id": "27840b09-727a-4ed1-babf-0144ad75aa56"
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"a=(1,)\n",
"a"
],
"id": "55eb0c21-445a-4760-857c-0fc6b6171dec"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 연산\n",
"\n",
"`-` 리스트와 동일"
],
"id": "5f99c8c8-b049-4403-b7b6-21c4b93e2f04"
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"(1,2)+(3,4,5)"
],
"id": "b6ff916d-730d-4e67-8e1d-a4b9f6fd53ce"
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"(1,2)*2"
],
"id": "e3ed0bcf-930d-477e-bc82-7cca92d2840f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 인덱싱\n",
"\n",
"`-` 리스트와 동일"
],
"id": "f7b7c859-a0f9-4b56-890c-f30315a67881"
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"a=(1,2,3,-4,-5)\n",
"a"
],
"id": "37139ed0-52e9-445e-b758-4c0812db0039"
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"a[-1]"
],
"id": "d576f437-7fdb-4a74-9bdf-f28118d78d1d"
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
"a[-3:]"
],
"id": "4ddf097a-53fc-4c47-b35d-653ba121135a"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# HW 0320\n",
"\n",
"`1`. 아래의 코드를 관찰하고, `sum()`의 기능을 유추하라."
],
"id": "56de0e7f-6464-422b-8bab-0016f56b08bb"
},
{
"cell_type": "code",
"execution_count": 252,
"metadata": {},
"outputs": [],
"source": [
"sum([1,0,1,0])"
],
"id": "ef5c25ac-d809-42da-b6f7-a7c487c7bd4d"
},
{
"cell_type": "code",
"execution_count": 253,
"metadata": {},
"outputs": [],
"source": [
"sum([True,False,True,False])"
],
"id": "6c2a11ea-02f5-460c-ab2e-851f65013c08"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)\n",
"\n",
"생략\n",
"\n",
"`2`. 다음과 같은 리스트를 고려하자."
],
"id": "97a9f59b-fa9d-48a5-9f7f-9dbc84615e30"
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [],
"source": [
"x = [80,60,80,90,55,85,95,100,35,70,75,65,95]\n",
"x"
],
"id": "71f2cfc2-5cb1-441a-a7aa-51477b44913a"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"이를 수식으로 표현하면 아래와 같다.\n",
"\n",
"${\\bf x} = [x_1,\\dots,x_{13}]=[80,60,80,90,55,85,95,100,35,70,75,65,95]$\n",
"\n",
"리스트의 원소중 “$x_i>80$” 의 조건을 만족하는 원소는 모두 몇개인가?\n",
"\n",
"**hint:** 리스트컴프리헨션과 `sum()`함수를 이용할 것\n",
"\n",
"(풀이)"
],
"id": "eb13c1c1-8e6c-442c-9b6d-15275f3e0e8e"
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [],
"source": [
"sum([xi >80 for xi in x])"
],
"id": "775fce4b-34a2-4d36-86b6-9fee6b97a8dd"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`3`. 다음과 같은 리스트를 고려하자."
],
"id": "8c367297-0b5f-4ef3-840a-4f735b68ac7f"
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [],
"source": [
"['A','B','C','D','A','A','B','A','F','C','C','C','A']"
],
"id": "2c34c9ea-78c3-446e-90ba-535fb8a575dd"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"이 리스트에서 ‘A’ 혹은 ’B’의 숫자는 모두 몇개인가?\n",
"\n",
"**hint:** 아래를 관찰"
],
"id": "8436a75a-fc8d-42be-b161-c82a9472e283"
},
{
"cell_type": "code",
"execution_count": 259,
"metadata": {},
"outputs": [],
"source": [
"'A' < 'C'"
],
"id": "8c915127-6fce-4c25-baeb-bcedfbbcbb82"
},
{
"cell_type": "code",
"execution_count": 260,
"metadata": {},
"outputs": [],
"source": [
"'B' < 'C'"
],
"id": "245ab8c8-1cf5-4636-839a-0d0364530340"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "29933d85-b2f1-45f5-bcc2-f77f158ed9ee"
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [],
"source": [
"lst = ['A','B','C','D','A','A','B','A','F','C','C','C','A']\n",
"sum([l <'C' for l in lst])"
],
"id": "19c1b475-14f3-46eb-b435-6f7ee6f301d5"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`4`. 아래와 같은 리스트가 있다고 하자.\n",
"\n",
"${\\bf x} = [1,2,1,5,6,2,4,7]$\n",
"\n",
"${\\bf y} = [3,2,4,1,2,5,6,7]$\n",
"\n",
"이러한 벡터를 파이썬에서 표현하기 위해서 아래와 같은 리스트를 만들었다고\n",
"하자."
],
"id": "e9acd0a8-292f-496c-852e-b108a837e899"
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [],
"source": [
"x=[1,2,1,5,6,2,4,7]\n",
"y=[3,2,4,1,2,5,6,7] "
],
"id": "f3a34352-7812-4b12-b56d-79748a20ae86"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"리스트컴프리헨션을 이용하여\n",
"\n",
"$${\\bf z}=[x_1^2+y_1^2, \\dots, x_{8}^2+y_{8}^2]=[x_i^2+y_i^2: \\text{for $i = 1,2,3,\\dots,8$}]$$\n",
"\n",
"와 같은 리스트를 생성하라.\n",
"\n",
"(풀이)"
],
"id": "e9b28661-6091-4c92-bffe-cafba65fb736"
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [],
"source": [
"[x[i]**2+y[i]**2 for i in range(8)]"
],
"id": "a5d9b0e2-77e4-4df1-b410-c42eed130162"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`5`. 아래와 같은 문자열이 있다고 하자."
],
"id": "a23661bd-a1b8-4190-92f7-c707d63a1641"
},
{
"cell_type": "code",
"execution_count": 64,
"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": "b0f440a2-b1a5-4189-bc24-59a135f50dc9"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"이 문자열에서 대문자의 수를 count하라. (2022년 파이썬입문 중간고사 1-(5)\n",
"참고)\n",
"\n",
"(풀이)"
],
"id": "4086b70b-7358-474f-b92c-6f009b8e3c54"
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [],
"source": [
"sum([s.isupper() for s in test_arr ])"
],
"id": "337dd41d-f37b-491e-9e2a-01f0dbe23399"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`6`. 리스트컴프리헨션을 이용하여 아래와 같은 리스트를 생성하라. (2022년\n",
"파이썬입문 중간고사 1-(7) 참고)\n",
"\n",
" ['a',\n",
" 'aa',\n",
" 'aaa',\n",
" 'aaaa',\n",
" 'aaaaa',\n",
" 'aaaaaa',\n",
" 'aaaaaaa',\n",
" 'aaaaaaaa',\n",
" 'aaaaaaaaa',\n",
" 'aaaaaaaaaa'] <- a가 10개있음\n",
"\n",
"(풀이)"
],
"id": "ec03a4ab-d533-4c71-8639-6c94f931936a"
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [],
"source": [
"['a'*i for i in range(1,11)]"
],
"id": "05181ed4-22a1-47ea-ae4d-1d2b6f9b924c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`7`. 아래와 같은 list가 있다고 하자."
],
"id": "3007657e-8640-4726-890f-5b8f48128f60"
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
"test_lst = [['g',1],['u',5],['e',2],['b',8],['i',2],['n',9]]"
],
"id": "4732adb0-2093-4389-92df-eb04b72a7e18"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`test_lst`와 리스트컴프리헨션을 이용하여 아래를 출력하는 코드를\n",
"구현하라. (2022년 파이썬입문 중간고사 1-(9) 참고)"
],
"id": "c50aaf7c-23d5-4b92-a4cf-48118b7087dc"
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
"['g', 'uuuuu', 'ee', 'bbbbbbbb', 'ii', 'nnnnnnnnn']"
],
"id": "bc06e562-130f-4bd1-a9da-2e12f75ec1fc"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "b58c034c-ea96-4c00-8849-dec73bb005ab"
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [],
"source": [
"[l[0]*l[1] for l in test_lst]"
],
"id": "b125b31b-7600-4ca8-ae13-769191d8f4b7"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`8`. 아래와 같은 list를 생성하라.\n",
"\n",
"``` python\n",
"[1,\n",
" 2,2,\n",
" 3,3,3,\n",
" 4,4,4,4,\n",
" 5,5,5,5,5,\n",
" ...\n",
" 9,9,9,9,9,9,9,9,9] <- 9가 9개 있음 \n",
"```\n",
"\n",
"(풀이1) – 의도한 풀이"
],
"id": "644639fc-fa94-4d47-9192-749100ebe08b"
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"lst = list()\n",
"for i in range(1,10):\n",
" lst = lst + [i]*i"
],
"id": "7864a095-f3be-4bce-9970-e5b2be21e822"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이2) – 이걸 의도한건 아니었음…"
],
"id": "eff32bd0-a259-401a-b245-d9d86ad93fe3"
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [],
"source": [
"lst = [[i]*i for i in range(1,10)]"
],
"id": "a0dfba8f-8640-4413-be5a-27ae9cb648c7"
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[1,\n",
" 2,2,\n",
" 3,3,3,\n",
" 4,4,4,4,\n",
" 5,5,5,5,5,\n",
" 6,6,6,6,6,6,\n",
" 7,7,7,7,7,7,7,\n",
" 8,8,8,8,8,8,8,8,\n",
" 9,9,9,9,9,9,9,9,9]\n",
" "
]
}
],
"source": [
"print(str(lst).replace(' ','').replace(',[','').replace(']',',\\n ').replace('[[','[').replace(',\\n ,',']'))"
],
"id": "812ba20e-2625-4374-8ff8-400a2762babf"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`9`. 아래와 같은 리스트를 관찰하라."
],
"id": "4ca3893c-c9e3-4dd7-bd19-e81e720e56fa"
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
"lst = ['2022/09/21','2022/10/30','2022/12/25','2023/01/01','2023/01/31','2023/03/20']"
],
"id": "0e3ea8bb-559e-4b49-942e-a51af107c77f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"이러한 리스트를 아래와 같은 리스트로 변환하는 코드를 작성하라."
],
"id": "d9abe1b9-42fe-461d-92d5-ee4def66c1be"
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
"['2022-09-21', '2022-10-30', '2022-12-25', '2023-01-01', '2023-01-31', '2023-03-20']"
],
"id": "f704c0d2-45ba-46b2-9057-5dd0d79f2abf"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**hint: string의 `.replace()`기능과 리스트 컴프리헨션의 응용**\n",
"\n",
"(풀이)"
],
"id": "6c430447-41bb-453f-80aa-d5e4bb238644"
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"[s.replace('/','-') for s in lst]"
],
"id": "b05b957b-5d92-4384-964a-8a200f3f105c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`10`. 아래와 같은 문자열을 고려하라."
],
"id": "2fe253d8-9506-4e0f-8d90-95b3756ee3a7"
},
{
"cell_type": "code",
"execution_count": 165,
"metadata": {},
"outputs": [],
"source": [
"'2021. 01. 05.'"
],
"id": "bda58b54-0c51-4e76-9ac2-408108acf341"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"띄어쓰기를 제거하는 코드를 구현하라. 즉 출력결과가 아래와 같도록 만드는\n",
"코드를 구현하라."
],
"id": "06b514ce-11cd-45af-b274-3f864c46f9df"
},
{
"cell_type": "code",
"execution_count": 166,
"metadata": {},
"outputs": [],
"source": [
"'2021.01.05'"
],
"id": "f0a98f61-a7f5-413b-81b8-a65d0d11708b"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**hint**: 아래코드 관찰"
],
"id": "8835b16a-fb3c-483b-aca4-ad8c41e094ba"
},
{
"cell_type": "code",
"execution_count": 167,
"metadata": {},
"outputs": [],
"source": [
"'asdf'.replace('a','')"
],
"id": "091cb822-1a53-4b88-bf4d-6766fb9e9f72"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "758940b6-074e-4009-b9b8-ecd6f9ee2487"
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"'2021. 01. 05.'.replace(' ','')"
],
"id": "2f144b55-7b70-4e5f-bcf4-e7bc7f57fa0f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`11`. 아래의 코드를 관찰하라."
],
"id": "f576383f-d8b5-47b8-a51a-d770961c6a75"
},
{
"cell_type": "code",
"execution_count": 168,
"metadata": {},
"outputs": [],
"source": [
"'-'.join(['2022','01','05'])"
],
"id": "03d85864-74ad-489d-b192-a533e6d840cc"
},
{
"cell_type": "code",
"execution_count": 169,
"metadata": {},
"outputs": [],
"source": [
"'.'.join(['2022','01','05'])"
],
"id": "67bca674-031f-461c-a2e4-6443f5a3a840"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ChatGPT를 이용하여 이 코드가 의미하는 내용을 유추하라.\n",
"\n",
"(풀이)\n",
"\n",
"생략\n",
"\n",
"`12`. 아래와 같이 하나의 특수문자로 이루어진 리스트를 고려하자."
],
"id": "844db652-7a13-4489-8a45-d0a4b11bb5ea"
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"block = ['■'] "
],
"id": "70230fbc-58ab-4cbd-a3de-ca803f1e0142"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"이를 이용하여 아래와 같은 결과를 출력하라."
],
"id": "f9949c80-1589-4e69-96c0-9e9c09ed47ea"
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
"'■-■-■-■-■-■-■-■-■-■' # 여기에서 '■'는 모두 10개 있음"
],
"id": "9375487f-9288-4c7b-b46c-18da10504f58"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**hint:** 11번 문제의 코드를 응용할 것\n",
"\n",
"(풀이)"
],
"id": "1582ac90-dd2d-4b81-bf36-44f452bda7de"
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
"'-'.join(block*10)"
],
"id": "c4fcb84a-bd64-4d91-a9b4-3899f17333e9"
}
],
"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"
}
}
}