{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 11. 시퀀스를 슬라이싱하는 방법을 익혀라." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "가운데 2개: ['d', 'e']\n", "마지막을 제외한 나머지: ['b', 'c', 'd', 'e', 'f', 'g']\n" ] } ], "source": [ "a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']\n", "print('가운데 2개: ', a[3:5])\n", "print('마지막을 제외한 나머지:', a[1:7])" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "assert a[:5] == a[0:5]\n", "assert a[5:] == a[5:len(a)]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[:] # ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c', 'd', 'e']" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[:5] # ['a', 'b', 'c', 'd', 'e']" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c', 'd', 'e', 'f', 'g']" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[:-1] # ['a', 'b', 'c', 'd', 'e', 'f', 'g']" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['e', 'f', 'g', 'h']" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[4:] # ['e', 'f', 'g', 'h']" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['f', 'g', 'h']" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[-3:] # ['f', 'g', 'h']" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['c', 'd', 'e']" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[2:5] # ['c', 'd', 'e']" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['c', 'd', 'e', 'f', 'g']" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[2:-1] # ['c', 'd', 'e', 'f', 'g']" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['f', 'g']" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[-3:-1] # ['f', 'g']" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "first_twenty_items = a[:20]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "last_twenty_items = a[-20:]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "이전: ['d', 'e', 'f', 'g', 'h']\n", "이후: ['d', 99, 'f', 'g', 'h']\n", "변화 없음: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']\n" ] } ], "source": [ "b = a[3:]\n", "print('이전:', b)\n", "b[1] = 99\n", "print('이후:', b)\n", "print('변화 없음:', a)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "이전: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']\n", "이후: ['a', 'b', 99, 22, 14, 'h']\n" ] } ], "source": [ "print('이전:', a)\n", "a[2:7] = [99, 22, 14]\n", "print('이후:', a)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "이전: ['a', 'b', 99, 22, 14, 'h']\n", "이후: ['a', 'b', 47, 11, 22, 14, 'h']\n" ] } ], "source": [ "print('이전:', a)\n", "a[2:3] = [47, 11]\n", "print('이후:', a)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "b = a[:]\n", "assert b == a and b is not a" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "이전 a: ['a', 'b', 47, 11, 22, 14, 'h']\n", "이전 b: ['a', 'b', 47, 11, 22, 14, 'h']\n", "이후 a: [101, 102, 103]\n", "이후 b: [101, 102, 103]\n" ] } ], "source": [ "b = a\n", "print('이전 a:', a)\n", "print('이전 b:', b)\n", "a[:] = [101, 102, 103]\n", "assert a is b # 여전히 같은 리스트 객체임\n", "print('이후 a:', a) # 새로운 내용이 들어 있음\n", "print('이후 b:', b) # 같은 리스트 객체이기 때문에 a와 내용이 같음" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 기억해야 할 내용\n", "- 슬라이싱할 때는 간결하게 하라. 시작 인덱스에 0을 넣거나, 끝 인덱스에 시퀀스 길이를 넣지 말라.\n", "- 슬라이싱은 범위를 넘어가는 시작 인덱스나 끝 인덱스도 허용한다. 따라서 시퀀스의 시작이나 끝에서 길이를 제한하는 슬라이스를 쉽게 표현할 수 있다.\n", "- 리스트 슬라이스에 대입하면 원래 시쿠너스에서 슬라이스가 가리키는 부분을 대입 연산자 오른쪽에 있는 시퀀스로 대치한다. 이때 슬라이스와 대치되는 시퀀스의 길이가 달라도 된다." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 4 }