|
39 | 39 | }, |
40 | 40 | { |
41 | 41 | "cell_type": "code", |
42 | | - "execution_count": null, |
| 42 | + "execution_count": 8, |
43 | 43 | "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 | + ], |
45 | 53 | "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", |
49 | 54 | "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)" |
53 | 60 | ] |
54 | 61 | }, |
55 | 62 | { |
|
105 | 112 | }, |
106 | 113 | { |
107 | 114 | "cell_type": "code", |
108 | | - "execution_count": null, |
| 115 | + "execution_count": 15, |
109 | 116 | "metadata": {}, |
110 | 117 | "outputs": [], |
111 | 118 | "source": [ |
112 | | - "# Think of this as the header row of your csv file\n", |
113 | 119 | "my_header_list = [\"a\", \"e\", \"b\", \"d\", \"c\", \"c\"]\n", |
114 | | - "# Think of this as the second row of your csv file\n", |
115 | 120 | "my_values_list = [0.3, 1.2, 5.2, 6.2, 3.2, 3.5]" |
116 | 121 | ] |
117 | 122 | }, |
118 | 123 | { |
119 | 124 | "cell_type": "code", |
120 | | - "execution_count": null, |
| 125 | + "execution_count": 21, |
121 | 126 | "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 | + ], |
123 | 136 | "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", |
126 | 137 | "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", |
129 | 138 | "print(f\"Sorted: {my_header_list_sorted}\")" |
130 | 139 | ] |
131 | 140 | }, |
|
145 | 154 | }, |
146 | 155 | { |
147 | 156 | "cell_type": "code", |
148 | | - "execution_count": null, |
| 157 | + "execution_count": 23, |
149 | 158 | "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 | + ], |
151 | 169 | "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}\")" |
163 | 176 | ] |
164 | 177 | }, |
165 | 178 | { |
|
171 | 184 | }, |
172 | 185 | { |
173 | 186 | "cell_type": "code", |
174 | | - "execution_count": null, |
| 187 | + "execution_count": 5, |
175 | 188 | "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 | + ], |
177 | 200 | "source": [ |
178 | 201 | "print(\"#\" * 20 + \" ANSWERS \" + \"#\" * 20)\n", |
179 | 202 | "print(\"Version 1\")\n", |
|
210 | 233 | }, |
211 | 234 | { |
212 | 235 | "cell_type": "code", |
213 | | - "execution_count": null, |
| 236 | + "execution_count": 9, |
214 | 237 | "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 | + ], |
216 | 247 | "source": [ |
217 | 248 | "# Sorting returns a NEW list that is sorted\n", |
218 | 249 | "my_header_list_sorted.sort() # Sorts the list in-place\n", |
|
221 | 252 | }, |
222 | 253 | { |
223 | 254 | "cell_type": "code", |
224 | | - "execution_count": null, |
| 255 | + "execution_count": 10, |
225 | 256 | "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 | + ], |
227 | 267 | "source": [ |
228 | 268 | "# Step 1: Zip the two UNSORTED lists together, then sort\n", |
229 | 269 | "my_combined_lists = zip(my_header_list, my_values_list)\n", |
|
253 | 293 | "name": "python", |
254 | 294 | "nbconvert_exporter": "python", |
255 | 295 | "pygments_lexer": "ipython3", |
256 | | - "version": "3.7.4" |
| 296 | + "version": "3.10.13" |
257 | 297 | } |
258 | 298 | }, |
259 | 299 | "nbformat": 4, |
|
0 commit comments