Skip to content

Commit 1986bbc

Browse files
committed
up
1 parent 8369ece commit 1986bbc

2 files changed

Lines changed: 52 additions & 9 deletions

File tree

images/list-copy-06.png

45.6 KB
Loading

lists-tuples.ipynb

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
"id": "b7465925-f58b-4e43-ab1e-16230af1255d",
9696
"metadata": {},
9797
"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",
9999
"<br>"
100100
]
101101
},
@@ -2037,7 +2037,7 @@
20372037
"id": "0e2db984-ef8d-4b31-9628-ad2a17610d00",
20382038
"metadata": {},
20392039
"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",
20412041
"\n",
20422042
"<p><div style=\"text-align: center\">&lt;그림 출처: <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>&gt;</div></p>"
20432043
]
@@ -2056,7 +2056,7 @@
20562056
"id": "a258c46a-723f-4bd3-8328-4a866fd09774",
20572057
"metadata": {},
20582058
"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",
20602060
"\n",
20612061
"<p><div style=\"text-align: center\">&lt;그림 출처: <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>&gt;</div></p>"
20622062
]
@@ -2108,7 +2108,7 @@
21082108
"id": "74402f12-6f93-488b-b584-5077579aecc2",
21092109
"metadata": {},
21102110
"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",
21122112
"\n",
21132113
"<p><div style=\"text-align: center\">&lt;그림 출처: <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>&gt;</div></p>"
21142114
]
@@ -2127,7 +2127,7 @@
21272127
"id": "b2683bab-fe5d-4946-936d-3a9accb0cdfc",
21282128
"metadata": {},
21292129
"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",
21312131
"\n",
21322132
"<p><div style=\"text-align: center\">&lt;그림 출처: <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>&gt;</div></p>"
21332133
]
@@ -2151,7 +2151,7 @@
21512151
},
21522152
{
21532153
"cell_type": "code",
2154-
"execution_count": 59,
2154+
"execution_count": 61,
21552155
"id": "19bc883c",
21562156
"metadata": {},
21572157
"outputs": [
@@ -2170,17 +2170,17 @@
21702170
"y_nested[0][0] = 10 # 0번 인데스 항목 수정\n",
21712171
"\n",
21722172
"print(\"y_nested:\", y_nested) # 수정됨\n",
2173-
"print(\"x_nested:\", x_nested) # 불변"
2173+
"print(\"x_nested:\", x_nested) # 항목 리스트는 불변이 아님"
21742174
]
21752175
},
21762176
{
21772177
"cell_type": "markdown",
21782178
"id": "0d4bed30",
21792179
"metadata": {},
21802180
"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",
21822182
"\n",
2183-
"<p><div style=\"text-align: center\">&lt;그림 출처: <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>&gt;</div></p>"
2183+
"<p><div style=\"text-align: center\">&lt;그림 출처: <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>&gt;</div></p>"
21842184
]
21852185
},
21862186
{
@@ -2193,6 +2193,49 @@
21932193
"이 사실을 아래 이미지가 잘 보여준다."
21942194
]
21952195
},
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+
},
21962239
{
21972240
"cell_type": "markdown",
21982241
"id": "e8908426-1d2b-4fc3-a5c9-3a839cdfa78f",

0 commit comments

Comments
 (0)