Skip to content

Commit 51af858

Browse files
committed
201 JLT
1 parent ff82209 commit 51af858

1 file changed

Lines changed: 77 additions & 37 deletions

File tree

‎Week_0_python/e_practice_list.ipynb‎

Lines changed: 77 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,24 @@
3939
},
4040
{
4141
"cell_type": "code",
42-
"execution_count": null,
42+
"execution_count": 8,
4343
"metadata": {},
44-
"outputs": [],
44+
"outputs": [
45+
{
46+
"name": "stdout",
47+
"output_type": "stream",
48+
"text": [
49+
"[0.3666666666666667, 0.6333333333333333, 0.9, 1.0333333333333332]\n"
50+
]
51+
}
52+
],
4553
"source": [
46-
"# Once this works, try adding in more numbers to my_list_numbers to check that it still works\n",
47-
"my_list_numbers = [0.1, 0.2, 0.8, 0.9, 1.0, 1.2]\n",
48-
"\n",
4954
"my_list_averaged = []\n",
50-
"for v in my_list_numbers:\n",
51-
" my_list_averaged.append(v)\n",
52-
"print(f\"Averaged list: {my_list_averaged}\")"
55+
"for i, v in enumerate(my_list_numbers):\n",
56+
" if 0 < i < len(my_list_numbers) - 1: \n",
57+
" val = (my_list_numbers[i-1] + v + my_list_numbers[i+1]) / 3\n",
58+
" my_list_averaged.append(val)\n",
59+
"print(my_list_averaged)"
5360
]
5461
},
5562
{
@@ -105,27 +112,29 @@
105112
},
106113
{
107114
"cell_type": "code",
108-
"execution_count": null,
115+
"execution_count": 15,
109116
"metadata": {},
110117
"outputs": [],
111118
"source": [
112-
"# Think of this as the header row of your csv file\n",
113119
"my_header_list = [\"a\", \"e\", \"b\", \"d\", \"c\", \"c\"]\n",
114-
"# Think of this as the second row of your csv file\n",
115120
"my_values_list = [0.3, 1.2, 5.2, 6.2, 3.2, 3.5]"
116121
]
117122
},
118123
{
119124
"cell_type": "code",
120-
"execution_count": null,
125+
"execution_count": 21,
121126
"metadata": {},
122-
"outputs": [],
127+
"outputs": [
128+
{
129+
"name": "stdout",
130+
"output_type": "stream",
131+
"text": [
132+
"Sorted: ['a', 'e', 'b', 'd', 'c', 'c']\n"
133+
]
134+
}
135+
],
123136
"source": [
124-
"# First try - sort my_header_list to be in alphabetical order\n",
125-
"# Copy over the list, since sorting edits in place\n",
126137
"my_header_list_sorted = my_header_list.copy()\n",
127-
"# Now sort it\n",
128-
"# TODO: Sort my_header_list_sorted (Google \"sort on python lists\")\n",
129138
"print(f\"Sorted: {my_header_list_sorted}\")"
130139
]
131140
},
@@ -145,21 +154,25 @@
145154
},
146155
{
147156
"cell_type": "code",
148-
"execution_count": null,
157+
"execution_count": 23,
149158
"metadata": {},
150-
"outputs": [],
159+
"outputs": [
160+
{
161+
"name": "stdout",
162+
"output_type": "stream",
163+
"text": [
164+
"Sorted iterable [('a', 0.3), ('b', 5.2), ('c', 3.2), ('c', 3.5), ('d', 6.2), ('e', 1.2)]\n",
165+
"Second list, sorted by first: [0.3, 5.2, 3.2, 3.5, 6.2, 1.2]\n"
166+
]
167+
}
168+
],
151169
"source": [
152-
"# A CAUTIONARY NOTE\n",
153-
"# NOTE: Do NOT do something like this - it SEEMS like it will work - and it will - but only if there are no\n",
154-
"# duplicate elements... Also, it's really inefficient.\n",
155-
"# Count how many elements there are in the my_sort_second_list_wrong - why are there so many?\n",
156-
"my_sort_second_list_wrong = []\n",
157-
"for h in sorted(my_header_list):\n",
158-
" for i, orig_h in enumerate(my_header_list):\n",
159-
" if h is orig_h:\n",
160-
" my_sort_second_list_wrong.append(my_values_list[i])\n",
161-
"print(f\"Wrong answer: {my_sort_second_list_wrong}\")\n",
162-
"# END A CAUTIONARY NOTE"
170+
"my_combined_lists = zip(my_header_list, my_values_list)\n",
171+
"my_combined_lists_sorted = sorted(zip(my_header_list, my_values_list))\n",
172+
"my_value_sorted = [hv[1] for hv in my_combined_lists_sorted]\n",
173+
"\n",
174+
"print(f\"Sorted iterable {my_combined_lists_sorted}\")\n",
175+
"print(f\"Second list, sorted by first: {my_value_sorted}\")"
163176
]
164177
},
165178
{
@@ -171,9 +184,19 @@
171184
},
172185
{
173186
"cell_type": "code",
174-
"execution_count": null,
187+
"execution_count": 5,
175188
"metadata": {},
176-
"outputs": [],
189+
"outputs": [
190+
{
191+
"name": "stdout",
192+
"output_type": "stream",
193+
"text": [
194+
"#################### ANSWERS ####################\n",
195+
"Version 1\n",
196+
"[0.3666666666666667, 0.6333333333333333, 0.9, 1.0333333333333332]\n"
197+
]
198+
}
199+
],
177200
"source": [
178201
"print(\"#\" * 20 + \" ANSWERS \" + \"#\" * 20)\n",
179202
"print(\"Version 1\")\n",
@@ -210,9 +233,17 @@
210233
},
211234
{
212235
"cell_type": "code",
213-
"execution_count": null,
236+
"execution_count": 9,
214237
"metadata": {},
215-
"outputs": [],
238+
"outputs": [
239+
{
240+
"name": "stdout",
241+
"output_type": "stream",
242+
"text": [
243+
"Sorted: ['a', 'b', 'c', 'c', 'd', 'e']\n"
244+
]
245+
}
246+
],
216247
"source": [
217248
"# Sorting returns a NEW list that is sorted\n",
218249
"my_header_list_sorted.sort() # Sorts the list in-place\n",
@@ -221,9 +252,18 @@
221252
},
222253
{
223254
"cell_type": "code",
224-
"execution_count": null,
255+
"execution_count": 10,
225256
"metadata": {},
226-
"outputs": [],
257+
"outputs": [
258+
{
259+
"name": "stdout",
260+
"output_type": "stream",
261+
"text": [
262+
"Sorted iterable [('a', 0.3), ('b', 5.2), ('c', 3.2), ('c', 3.5), ('d', 6.2), ('e', 1.2)]\n",
263+
"Second list, sorted by first: [0.3, 5.2, 3.2, 3.5, 6.2, 1.2]\n"
264+
]
265+
}
266+
],
227267
"source": [
228268
"# Step 1: Zip the two UNSORTED lists together, then sort\n",
229269
"my_combined_lists = zip(my_header_list, my_values_list)\n",
@@ -253,7 +293,7 @@
253293
"name": "python",
254294
"nbconvert_exporter": "python",
255295
"pygments_lexer": "ipython3",
256-
"version": "3.7.4"
296+
"version": "3.10.13"
257297
}
258298
},
259299
"nbformat": 4,

0 commit comments

Comments
 (0)