|
95 | 95 | "id": "b7465925-f58b-4e43-ab1e-16230af1255d", |
96 | 96 | "metadata": {}, |
97 | 97 | "source": [ |
98 | | - "<div align=\"center\" border=\"1px\"><img src=\"https://raw.githubusercontent.com/codingalzi/42H/master/jupyter-book/images/built-in-types.png\" width=\"350\"/></div>\n", |
| 98 | + "<div align=\"center\" border=\"1px\"><img src=\"https://raw.githubusercontent.com/codingalzi/code-workout-python/master/images/built-in-types.png\" width=\"350\"/></div>\n", |
99 | 99 | "<br>" |
100 | 100 | ] |
101 | 101 | }, |
|
2037 | 2037 | "id": "0e2db984-ef8d-4b31-9628-ad2a17610d00", |
2038 | 2038 | "metadata": {}, |
2039 | 2039 | "source": [ |
2040 | | - "<div align=\"center\" border=\"1px\"><img src=\"https://raw.githubusercontent.com/codingalzi/42H/master/jupyter-book/images/list-copy-01.png\" width=\"800\"/></div>\n", |
| 2040 | + "<div align=\"center\" border=\"1px\"><img src=\"https://raw.githubusercontent.com/codingalzi/code-workout-python/master/images/list-copy-01.png\" width=\"800\"/></div>\n", |
2041 | 2041 | "\n", |
2042 | 2042 | "<p><div style=\"text-align: center\"><그림 출처: <a href=\"https://pythontutor.com/render.html#code=x%20%20%3D%20%5B1,%202,%203%5D%0Ay%20%3D%20x.copy%28%29%0Ay%5B0%5D%20%3D%2010&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false\">Python Tutor</a>></div></p>" |
2043 | 2043 | ] |
|
2056 | 2056 | "id": "a258c46a-723f-4bd3-8328-4a866fd09774", |
2057 | 2057 | "metadata": {}, |
2058 | 2058 | "source": [ |
2059 | | - "<div align=\"center\" border=\"1px\"><img src=\"https://raw.githubusercontent.com/codingalzi/42H/master/jupyter-book/images/list-copy-02.png\" width=\"800\"/></div>\n", |
| 2059 | + "<div align=\"center\" border=\"1px\"><img src=\"https://raw.githubusercontent.com/codingalzi/code-workout-python/master/images/list-copy-02.png\" width=\"800\"/></div>\n", |
2060 | 2060 | "\n", |
2061 | 2061 | "<p><div style=\"text-align: center\"><그림 출처: <a href=\"https://pythontutor.com/render.html#code=x%20%20%3D%20%5B1,%202,%203%5D%0Ay%20%3D%20x.copy%28%29%0Ay%5B0%5D%20%3D%2010&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false\">Python Tutor</a>></div></p>" |
2062 | 2062 | ] |
|
2108 | 2108 | "id": "74402f12-6f93-488b-b584-5077579aecc2", |
2109 | 2109 | "metadata": {}, |
2110 | 2110 | "source": [ |
2111 | | - "<div align=\"center\" border=\"1px\"><img src=\"https://raw.githubusercontent.com/codingalzi/42H/master/jupyter-book/images/list-copy-03.png\" width=\"800\"/></div>\n", |
| 2111 | + "<div align=\"center\" border=\"1px\"><img src=\"https://raw.githubusercontent.com/codingalzi/code-workout-python/master/images/list-copy-03.png\" width=\"800\"/></div>\n", |
2112 | 2112 | "\n", |
2113 | 2113 | "<p><div style=\"text-align: center\"><그림 출처: <a href=\"https://pythontutor.com/render.html#code=x%20%20%3D%20%5B1,%202,%203%5D%0Ay%20%3D%20x%0Ay%5B0%5D%20%3D%2010&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false\">Python Tutor</a>></div></p>" |
2114 | 2114 | ] |
|
2127 | 2127 | "id": "b2683bab-fe5d-4946-936d-3a9accb0cdfc", |
2128 | 2128 | "metadata": {}, |
2129 | 2129 | "source": [ |
2130 | | - "<div align=\"center\" border=\"1px\"><img src=\"https://raw.githubusercontent.com/codingalzi/42H/master/jupyter-book/images/list-copy-04.png\" width=\"800\"/></div>\n", |
| 2130 | + "<div align=\"center\" border=\"1px\"><img src=\"https://raw.githubusercontent.com/codingalzi/code-workout-python/master/images/list-copy-04.png\" width=\"800\"/></div>\n", |
2131 | 2131 | "\n", |
2132 | 2132 | "<p><div style=\"text-align: center\"><그림 출처: <a href=\"https://pythontutor.com/render.html#code=x%20%20%3D%20%5B1,%202,%203%5D%0Ay%20%3D%20x%0Ay%5B0%5D%20%3D%2010&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false\">Python Tutor</a>></div></p>" |
2133 | 2133 | ] |
|
2151 | 2151 | }, |
2152 | 2152 | { |
2153 | 2153 | "cell_type": "code", |
2154 | | - "execution_count": 59, |
| 2154 | + "execution_count": 61, |
2155 | 2155 | "id": "19bc883c", |
2156 | 2156 | "metadata": {}, |
2157 | 2157 | "outputs": [ |
|
2170 | 2170 | "y_nested[0][0] = 10 # 0번 인데스 항목 수정\n", |
2171 | 2171 | "\n", |
2172 | 2172 | "print(\"y_nested:\", y_nested) # 수정됨\n", |
2173 | | - "print(\"x_nested:\", x_nested) # 불변" |
| 2173 | + "print(\"x_nested:\", x_nested) # 항목 리스트는 불변이 아님" |
2174 | 2174 | ] |
2175 | 2175 | }, |
2176 | 2176 | { |
2177 | 2177 | "cell_type": "markdown", |
2178 | 2178 | "id": "0d4bed30", |
2179 | 2179 | "metadata": {}, |
2180 | 2180 | "source": [ |
2181 | | - "<div align=\"center\" border=\"1px\"><img src=\"https://raw.githubusercontent.com/codingalzi/42H/master/jupyter-book/images/list-copy-04.png\" width=\"800\"/></div>\n", |
| 2181 | + "<div align=\"center\" border=\"1px\"><img src=\"https://raw.githubusercontent.com/codingalzi/code-workout-python/master/images/list-copy-05.png\" width=\"950\"/></div>\n", |
2182 | 2182 | "\n", |
2183 | | - "<p><div style=\"text-align: center\"><그림 출처: <a href=\"https://pythontutor.com/render.html#code=x%20%20%3D%20%5B1,%202,%203%5D%0Ay%20%3D%20x%0Ay%5B0%5D%20%3D%2010&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false\">Python Tutor</a>></div></p>" |
| 2183 | + "<p><div style=\"text-align: center\"><그림 출처: <a href=\"https://pythontutor.com/visualize.html#code=x_nested%20%20%3D%20%5B%5B1,%202,%203%5D,%20%5B4,%205,%206%5D%5D%0Ay_nested%20%3D%20x_nested.copy%28%29%0Ay_nested%5B0%5D%5B0%5D%20%3D%2010&curInstr=0&mode=display&origin=opt-frontend.js&py=3\">Python Tutor</a>></div></p>" |
2184 | 2184 | ] |
2185 | 2185 | }, |
2186 | 2186 | { |
|
2193 | 2193 | "이 사실을 아래 이미지가 잘 보여준다." |
2194 | 2194 | ] |
2195 | 2195 | }, |
| 2196 | + { |
| 2197 | + "cell_type": "markdown", |
| 2198 | + "id": "dbbdcc34", |
| 2199 | + "metadata": {}, |
| 2200 | + "source": [ |
| 2201 | + "**`copy.deepcopy()` 함수**" |
| 2202 | + ] |
| 2203 | + }, |
| 2204 | + { |
| 2205 | + "cell_type": "markdown", |
| 2206 | + "id": "8bf65448", |
| 2207 | + "metadata": {}, |
| 2208 | + "source": [ |
| 2209 | + "중첩 리스트의 항목까지 복제해서 사본을 만드려면 `copy` 모듈의 `deepcopy()` 함수를 활용한다.\n", |
| 2210 | + "아래 코드에서 확인할 수 있듯이 `copy.deepcopy()` 함수를 활용하면 완전히 독립된 사본이 생성된다." |
| 2211 | + ] |
| 2212 | + }, |
| 2213 | + { |
| 2214 | + "cell_type": "code", |
| 2215 | + "execution_count": 62, |
| 2216 | + "id": "f20d3959", |
| 2217 | + "metadata": {}, |
| 2218 | + "outputs": [ |
| 2219 | + { |
| 2220 | + "name": "stdout", |
| 2221 | + "output_type": "stream", |
| 2222 | + "text": [ |
| 2223 | + "y_nested: [[10, 2, 3], [4, 5, 6]]\n", |
| 2224 | + "x_nested: [[1, 2, 3], [4, 5, 6]]\n" |
| 2225 | + ] |
| 2226 | + } |
| 2227 | + ], |
| 2228 | + "source": [ |
| 2229 | + "import copy\n", |
| 2230 | + "\n", |
| 2231 | + "x_nested = [[1, 2, 3], [4, 5, 6]]\n", |
| 2232 | + "y_nested = copy.deepcopy(x_nested)\n", |
| 2233 | + "y_nested[0][0] = 10 # 0번 인데스 항목 수정\n", |
| 2234 | + "\n", |
| 2235 | + "print(\"y_nested:\", y_nested) # 수정됨\n", |
| 2236 | + "print(\"x_nested:\", x_nested) # 항목 리스트도 불변" |
| 2237 | + ] |
| 2238 | + }, |
2196 | 2239 | { |
2197 | 2240 | "cell_type": "markdown", |
2198 | 2241 | "id": "e8908426-1d2b-4fc3-a5c9-3a839cdfa78f", |
|
0 commit comments