{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 03wk-2: 파이썬의 자료형 (5)\n", "\n", "최규빈 \n", "2023-03-22\n", "\n", "\n", "\n", "# 튜플 고급내용\n", "\n", "## 튜플을 왜 쓸까? (1)\n", "\n", "`-` 책의 설명 (이 설명이 꼭 파이썬에 한정되는 것은 아님. 모든 언어에\n", "존재하는 불변형 객체에 적용가능한 설명)\n", "\n", "- 실수방지\n", "- 빠르다, 다중작업에 유리하다, 여러사람과 작업하기에 유리하다,\n", " 깊은복사/얕은복사시 원하지않는 오류(side effect이라고 함)를 방지할\n", " 수 있다, 메모리관리에도 유리함…\n", "- 느낌: 불변형은 기능제한이 있는데 가볍고 빠른, 가변형은 기능은\n", " 풍부하지만 약간 느리고 무거운 느낌임 (불변형:라면사리, 가변형:라면)\n", "\n", "## 슬기로운 튜플사용 ($\\star\\star\\star\\star\\star$)\n", "\n", "`-` 예제: 여러변수를 동시에 출력하고 싶을 경우 (다중출력?)\n", "\n", "변수를 아래와 같이 선언하였다고 하자." ], "id": "58164dd7-4663-4823-b35b-5048290efae7" }, { "cell_type": "code", "execution_count": 192, "metadata": {}, "outputs": [], "source": [ "a=1\n", "b=2\n", "c=3" ], "id": "99b06179-f86a-44a0-8f49-96d810816151" }, { "cell_type": "markdown", "metadata": {}, "source": [ "선언된 값을 확인하려면?" ], "id": "38eee2fb-6caa-42cf-b881-9fdc9d215fb4" }, { "cell_type": "code", "execution_count": 193, "metadata": {}, "outputs": [], "source": [ "a" ], "id": "ded7b310-8f1c-4229-b72b-907e0346b3a7" }, { "cell_type": "code", "execution_count": 194, "metadata": {}, "outputs": [], "source": [ "b" ], "id": "21a5f61f-8c76-464d-b057-76a741e43a1e" }, { "cell_type": "code", "execution_count": 195, "metadata": {}, "outputs": [], "source": [ "c" ], "id": "471137dc-5614-48f7-9b37-ed26fece0133" }, { "cell_type": "markdown", "metadata": {}, "source": [ "튜플을 이용하면?" ], "id": "f3515394-feca-48cd-86bf-8e74d283df46" }, { "cell_type": "code", "execution_count": 196, "metadata": {}, "outputs": [], "source": [ "a,b,c # 괄호하나 생략하는것이 이렇게 편하다.." ], "id": "37023a9e-d0d3-46df-bd8d-dd8d3cb835eb" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 예제: 다중할당1 (여러개의 변수를 동시에 선언하고 싶을 경우)" ], "id": "5b8316ab-94c5-49c1-9353-ae03be3d6042" }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [], "source": [ "name, age, sex, height, weight = 'Tom', 20, 'M', 180, 70 " ], "id": "bfaca07d-ae95-432d-ac22-cfd0d53045d3" }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [], "source": [ "name, age, sex, height, weight" ], "id": "1603cf23-b48e-405e-846a-3c033c50bc6d" }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "height" ], "id": "86769ec4-4f7e-4d13-9898-053f851042fa" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 예제: 다중할당2, 위도와 경도" ], "id": "8b92a221-f194-4df8-b640-10d56c7ea74a" }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [], "source": [ "coor = (37,127) # 서울 \n", "coor" ], "id": "fd4ce414-5bab-4d19-9352-e127d67a66d2" }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [], "source": [ "lat, long = coor" ], "id": "6261f723-914c-4ef6-8b01-41e4e7a84ee5" }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [], "source": [ "lat " ], "id": "b2869fd6-e4e6-4cbb-b13c-c6760d828838" }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [], "source": [ "long " ], "id": "cce692a6-4321-4e49-8853-fa07d1c4b7c8" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 잠깐만: 다중할당은 꼭 튜플에서만 가능한가?\n", "\n", "그건 아니다…" ], "id": "01d8d857-79ef-441c-913c-98ebf87f8c04" }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [], "source": [ "[x,y,z] = [1,2,3] \n", "x,y,z # 다중출력 " ], "id": "d67cea06-9942-4a84-920b-73d0f77d5674" }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [], "source": [ "[x,y] = 'hi'\n", "x,y " ], "id": "80682112-1652-47aa-9ec4-0b30b47569c8" }, { "cell_type": "markdown", "metadata": {}, "source": [ "튜플과 같이 사용하면 가독성이 극대화 (그래서 다중할당은 거의 튜플과\n", "세트로 사용함)" ], "id": "06d821a9-5920-45b1-90c0-09284243c0fe" }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [], "source": [ "x,y,z = 1,2,3\n", "x,y,z # 다중출력 " ], "id": "c713c296-1c1d-45df-bc79-a493bd0cb858" }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [], "source": [ "x,y = 'hi'\n", "x,y " ], "id": "e76c0343-4739-4ea3-aa40-d951ca6d084c" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 예제: 임시변수 사용없이 두 변수의 값을 교환" ], "id": "2a19d818-7238-400b-871b-df6fde01e97f" }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [], "source": [ "a=10\n", "b=20" ], "id": "0bb809cf-51c4-4d65-bf47-d935430b42d3" }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [], "source": [ "a,b = b,a " ], "id": "dbba3f0c-940d-4dc0-afbd-5a902e72eb19" }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [], "source": [ "a" ], "id": "17ea7f8b-9ceb-4713-b206-5cc7b7298374" }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [], "source": [ "b" ], "id": "6d6325a9-46d6-4b82-8397-9f5653246172" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 예제: for문과 튜플" ], "id": "ad9bf242-3558-44ec-82fa-d28af96ffd92" }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [], "source": [ "lst = [['guebin', 202112345, 'M'],\n", " ['iu',202254321, 'F'],\n", " ['hodong', 202011223, 'M']]\n", "lst" ], "id": "a403a286-7f1a-40a9-b38c-bbda662a6c5f" }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "guebin M\n", "iu F\n", "hodong M" ] } ], "source": [ "for name,studentid,sex in lst: \n", " print(name,sex)" ], "id": "4d5ce0cd-1737-4b37-a1aa-17841c8266f1" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 예제: for문과 튜플, dummy variable `_`" ], "id": "4e93c13f-71d5-4205-bdd0-be898641a044" }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "202112345\n", "202254321\n", "202011223" ] } ], "source": [ "for name,studentid,sex in lst: \n", " print(studentid)" ], "id": "328e16e5-5ed8-41b5-9762-e8290107d92c" }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "202112345\n", "202254321\n", "202011223" ] } ], "source": [ "for _,studentid,_ in lst: \n", " print(studentid)" ], "id": "9bb43f0f-1ce7-46a6-ba50-4c84449bbbb1" }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "M\n", "F\n", "M" ] } ], "source": [ "for _,_,sex in lst: \n", " print(sex)" ], "id": "26f12415-1764-4438-bf05-f7c0d4fe5458" }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "guebin M\n", "iu F\n", "hodong M" ] } ], "source": [ "for name,_,sex in lst: \n", " print(name,sex)" ], "id": "26d18442-93b7-487a-92ba-4c7328cde68d" }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [], "source": [ "for name,_ in lst: \n", " print(name)" ], "id": "d8bedc9b-6427-49fd-ad3a-c7a9e342c963" }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "guebin\n", "iu\n", "hodong" ] } ], "source": [ "for name,*args in lst: \n", " print(name)" ], "id": "7fe817c3-91be-4c90-9dd4-89641e43fb42" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 예제: 튜플과 언패킹연산자 `*`" ], "id": "f601e6b0-6eff-4024-ac61-07ab7b10d7f0" }, { "cell_type": "code", "execution_count": 181, "metadata": {}, "outputs": [], "source": [ "head, body, *tail = range(1,11) \n", "head, body, tail" ], "id": "64e67f19-81e5-4d97-a441-73a7198f66cb" }, { "cell_type": "code", "execution_count": 182, "metadata": {}, "outputs": [], "source": [ "head1,head2, *body, tail1,tail2,tail3 = range(1,11) \n", "head1,head2, body, tail1,tail2,tail3 " ], "id": "0d4d9bab-64db-4b16-b49a-64678b1e1ae4" }, { "cell_type": "code", "execution_count": 183, "metadata": {}, "outputs": [], "source": [ "*head, body, tail = range(1,11) \n", "head, body, tail" ], "id": "0d45beed-3267-4516-b7c3-53c003f3e78c" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(관찰)\n", "\n", "그러고 보니까..\n", "\n", "``` python\n", "head1,head2, body, tail1,tail2,tail3 = (1, 2, [3,4,5,6,7], 8, 9, 10)\n", "head1,head2, *body, tail1,tail2,tail3 = (1, 2, 3,4,5,6,7, 8, 9, 10)\n", "```\n", "\n", "이렇다는 거잖아?\n", "\n", "`*`를 붙이면 1차원 자료구조가 풀린다..?" ], "id": "ee25b046-9279-4f9e-bbb6-752d8d946ddf" }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "*[1,2,3]" ], "id": "d2f7084c-ee9b-4ef3-9857-4f3381dbb3b1" }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[1, 2, 3]" ] } ], "source": [ "print([1,2,3])" ], "id": "6a5d352a-0611-47af-89ac-3b2fc49e93c7" }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1 2 3" ] } ], "source": [ "print(*[1,2,3]) ## 이런 느낌이란 말이지.." ], "id": "1b31b179-c45c-4de6-b982-8acf099c363b" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 튜플을 왜 쓸까? (2)\n", "\n", "`-` 책의 설명 (이 설명이 꼭 파이썬에 한정되는 것은 아님. 모든 언어에\n", "존재하는 불변형 객체에 적용가능한 설명)\n", "\n", "- 실수방지\n", "- 빠르다, 다중작업에 유리하다, 여러사람과 작업하기에 유리하다,\n", " 깊은복사/얕은복사시 원하지않는 오류(side effect이라고 함)를 방지할\n", " 수 있다, 메모리관리에도 유리함…\n", "- 느낌: 불변형은 기능제한이 있는데 가볍고 빠른, 가변형은 기능은\n", " 풍부하지만 약간 느리고 무거운 느낌임 (불변형:라면사리, 가변형:라면)\n", "\n", "`-` 내 설명: 소괄화 생략할 수 있어서 쓰는거야\n", "\n", "- 튜플의 장점은 소괄호의 생략에 있음 (이것은 파이썬과 줄리아만 가능)\n", "- 소괄호생략 + 언패킹 $\\Rightarrow$ 엄청난 가독성\n", "- 컴공과 사람들 의견: 튜플 + 언패킹 $\\Rightarrow$ 엄청난 가독성\n", " $\\Rightarrow$ 충격 $\\Rightarrow$ “파이썬 편하더라고요..”" ], "id": "b108df28-7602-47ec-8692-d21803485a2e" }, { "cell_type": "code", "execution_count": 206, "metadata": {}, "outputs": [], "source": [ "def mycal(a,b):\n", " return a+b, a-b, a*b, a/b #여러개의 값을 리턴하는듯 보임. -> 사실은 길이가 4인 튜플 1개를 리턴" ], "id": "9797b464-b0fa-45ae-9905-9a42e135c33f" }, { "cell_type": "code", "execution_count": 207, "metadata": {}, "outputs": [], "source": [ "mycal(2,3)" ], "id": "1bba2a64-3eb8-4e11-b304-5c724c8d7df6" }, { "cell_type": "code", "execution_count": 208, "metadata": {}, "outputs": [], "source": [ "_, _, mulrslt, _ = mycal(2,3) # 병렬할당 " ], "id": "196fb8c7-160f-4815-bd98-a6fd93f2dbfc" }, { "cell_type": "code", "execution_count": 209, "metadata": {}, "outputs": [], "source": [ "mulrslt" ], "id": "723439bd-5607-4ccd-8b8a-d28975377291" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 의문: 왜 튜플만 괄호를 생략할 수 있지?\n", "\n", "답이 없는 문제인데 답을 해보겠습니다.\n", "\n", "- 튜플을 먼저 만들고, 괄호를 생략하는 문법을 추가한것은 아닐것임\n", "- 원래 괄호없이 컴마만 대충찍어서 선언가능한 아주간단한 타입의\n", " 벡터형을 만들고 싶었을 것임.\n", "- 왜? 괄호없는 벡터를 만들고, 언패킹을 사용하면 여러가지 구문들이\n", " 엄청나게 간단해짐.\n", "- 컴마컴마로 선언하는 벡터는 한 두번 쓰고 버리는 경우가 많으며 대부분\n", " 이름도 필요없음 $\\to$ 원소에 접근해서 sorting하여 순서를 바꾸고\n", " 싶다던가 원소를 추가할 이유가 없음 $\\to$ 비싼 가변형으로 만들 이유가\n", " 없다는 것..\n", "- 우리가 필요한 것: 데이터가 벡터의 형태로 모여있기만 하면 된다!\n", "\n", "# HW: 03-22 (2)\n", "\n", "`1`. 길이가 1인 튜플을 만들어 자신의 학번을 저장하라. 길이가 1인 튜플을\n", "만들어 자신의 영문이름을 저장하라. 두 튜플을 + 연산자로 합쳐아래와 같은\n", "출력결과를 얻어라. 최종 결과는 예를들면 아래와 같아야 한다." ], "id": "315b8a6d-6124-486e-b1c4-62b3d1921dc3" }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [], "source": [ "('2021-43052', 'GuebinChoi')" ], "id": "896fee29-7d76-4cf4-a98e-6e4dfc155852" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(풀이)" ], "id": "c8b9557e-94b4-4f01-b1ab-abc29baab05f" }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "a='2021-43052', \n", "b='GuebinChoi',\n", "a+b" ], "id": "19b40fb9-85bb-4a90-8008-01ff74f0e2b0" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`2-5`. 아래는 파이썬프로그래밍 수강생들의 학번, 이름, 출석점수,\n", "과제점수, 중간고사점수, 기말고사점수를 저장한 중첩리스트이다." ], "id": "b9762a29-da8c-40ea-81bd-79957e5dc2d8" }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "lst = [['2021-43052', 'GuebinChoi', 5, 10, 20, 25],\n", " ['2019-12342', 'Heung-min Son', 10, 15, 30, 15],\n", " ['2018-32234', 'hynn', 7, 20, 30, 15],\n", " ['2022-42323', 'Minji', 8, 20, 20, 35],\n", " ['2023-55342', 'Hanni', 7, 20, 30, 35],\n", " ['2022-46624', 'Danielle', 3, 15, 30, 40],\n", " ['2022-11239', 'Haerin', 10, 20, 30, 40],\n", " ['2022-32114', 'Hyein', 10, 20, 20, 35]]\n", "lst " ], "id": "c325957a-649b-472d-a0a6-6d821504ce53" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`2`. 파이썬프로그래밍 수강생의 수는 모두 몇명인가?\n", "\n", "(풀이)" ], "id": "c7d0ca27-f317-48d2-a1e4-2ae03df1f7f3" }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "len(lst)" ], "id": "45fa6605-a6ff-469b-842c-5b467ce909b5" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`3`. 전북대학교 지침에 따라 출석점수가 7보다 작은 학생은 (즉\n", "`출석점수 < 7` 일 경우) F학점을 부여하게 되어있다. 이 기준에 따르면 F를\n", "받는 학생은 모두 몇명인가?\n", "\n", "(풀이)" ], "id": "33840575-ffbb-4832-89c7-08b0da702a1e" }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "sum([att<7 for _,_, att, *rest in lst])" ], "id": "9df8ab11-80b6-4683-9713-5217b3bc2be8" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`4`. 파이썬프로그래밍 수업의 경우 `출석+레포트 < 21` 일 경우 F학점을\n", "부여한다고 한다. 이 기준에 따르면 F를 받는 학생은 모두 몇명인가?\n", "\n", "(풀이)" ], "id": "5a27b95b-59cb-4ac8-accc-0ac92c880692" }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "sum([att+rep < 21 for _,_, att,rep, *rest in lst])" ], "id": "fb117da0-9afd-47ff-a0db-4fed3490ee22" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`5`. 리스트의 정렬순서를 \\[학번, 이름, …, 기말고사점수\\] 가 아니라\n", "\\[이름, 학번, … , 기말고사점수\\] 와 같이 되도록 변경하는 코드를\n", "작성하라.\n", "\n", "(출력예시)" ], "id": "99010349-6012-4f5f-9109-9da4b3c701e1" }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "[[name,studentid,*rest] for studentid, name, *rest in lst]" ], "id": "c9f4b218-04e7-4205-af65-d0da760bd11a" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`6`. 아래의 코드를 관찰하라." ], "id": "49a87e04-b4fd-4e15-8c1f-7e0279657bea" }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [], "source": [ "'2023-03-22'.split('-')" ], "id": "b778d44c-9aa5-4d3d-a4c6-a02d5b962c23" }, { "cell_type": "markdown", "metadata": {}, "source": [ "이 코드를 바탕으로 split의 기능을 유추하라. (ChatGPT 활용해도 무방함)\n", "\n", "(풀이)\n", "\n", "생략..\n", "\n", "`7`. 6의 실행결과를 되돌리는 코드를 작성하라. 즉" ], "id": "15692a93-ad07-4bd9-bd9c-58da7d997d2f" }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [], "source": [ "['2023', '03', '22']" ], "id": "0252c65d-04d7-41f4-98b3-fa553b851f9e" }, { "cell_type": "markdown", "metadata": {}, "source": [ "와 같은 리스트를 아래의 string으로 바꾸는 코드를 작성하라." ], "id": "dc958980-a47a-496f-95d2-7511eb3323f7" }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [], "source": [ "'2023-03-22'" ], "id": "e94b7220-827d-49fa-bb38-49a39d232b3a" }, { "cell_type": "markdown", "metadata": {}, "source": [ "**hint:** join을 이용할 것\n", "\n", "(풀이)" ], "id": "b9d03cba-55da-45d8-bed2-5604ad9c0163" }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "'-'.join(['2023', '03', '22'])" ], "id": "004c7348-8342-4f28-b379-8206f444265d" }, { "cell_type": "markdown", "metadata": {}, "source": [ "------------------------------------------------------------------------\n", "\n", "다음은 인터넷에서 긁어온 어떠한 텍스트이다." ], "id": "70842151-862a-44ba-909b-6611cf206ca8" }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "text = \"국내뿐 아니라 해외 인기도 심상치 않다. 2023년 1월 18일 'Ditto'가 빌보드 핫 100에 96위로 진입했다. 이는 K-pop 역사상 데뷔후 최단 빌보드 Hot 100 차트 입성 기록이다. 다른 뮤지션들이 보통 데뷔 후 수년간 쌓아온 팬덤을 기반으로 빌보드에 입성한데 비해, 뉴진스의 기록은 이례적인 것으로 평가받고 있다. 또한 'OMG'가 빌보드 핫 100에 91위로 진입한 동시에 'Ditto'는 85위로 순위가 상승, 핫 100 주간차트에 두 곡을 올려놓았다. K-Pop 역사상 이 차트에 두 곡 이상을 진입시킨 아티스트는 방탄소년단과 블랙핑크가 유일하다. 'Ditto'는 1월 셋째주 기준, 빌보드뿐만 아니라 영국 오피셜 싱글 차트 '톱 100'에 2주 연속 진입하기도 했다.\"" ], "id": "24b3d09b-dcd8-4a75-8d70-e1d0ce6d15bf" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`8`. text는 총 몇개의 문장으로 이루어져 있는가?\n", "\n", "**hint:** 이 텍스트의 문장은 모두 `.`로 끝난다.\n", "\n", "(풀이)" ], "id": "336b9854-a8e8-4857-897e-059fd3a632bf" }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "lst = text.split('. ')\n", "lst " ], "id": "405cbe58-3820-4a5e-bd92-3a76d82e6fa5" }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "len(lst)" ], "id": "83706811-e71c-4040-9d27-090e1a8c3223" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`9`. 각 문장은 몇개의 단어로 이루어져 있는가?\n", "\n", "**hint:** 각 단어는 공백으로 구분된다.\n", "\n", "(풀이)" ], "id": "798cc704-b731-42a3-9bbb-c0aa192016c5" }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "[len(s.split(' ')) for s in lst]" ], "id": "c577144a-e7a8-4a33-8f2b-2641f2358b37" }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "sum([len(s.split(' ')) for s in lst])" ], "id": "5105d553-d7bf-489f-80a7-ef5d8cbe626d" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`10`. `100`이라는 단어가 포함된 문장은 모두 몇 개 인가?\n", "\n", "**hint:** 아래의 코드를 관찰" ], "id": "8ed0167d-8d28-4827-954e-4217caeaba26" }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [], "source": [ "'a' in 'abcd'" ], "id": "0479e392-96cc-4584-be91-520282303093" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(풀이)" ], "id": "5de1b3c5-6729-499e-a0f3-0dbe5c8a1e6e" }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "['100' in s for s in lst]" ], "id": "f39c64b1-b2d1-4352-a641-173456cb4449" }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "sum(['100' in s for s in lst])" ], "id": "d675ec48-4f83-4051-bb3d-c5ab9c74cf2c" } ], "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" } } }