{ "cells": [ { "cell_type": "markdown", "id": "54ee872b-aa95-4e8a-b22f-7a42c7d3c0cf", "metadata": {}, "source": [ "# Thinking in list comprehensions\n", "\n", "Here is a way of thinking about list comprehensions that may help you write more complex comprehensions in a natural way:" ] }, { "cell_type": "code", "execution_count": 1, "id": "80df68a2-5a72-4b44-9c72-8dffffc57dfe", "metadata": {}, "outputs": [], "source": [ "def make_powers_lc(n, inputs):\n", " \"\"\"\n", " Given list of inputs, return each input raised to the 1st-Nth power\n", " \n", " e.g.\n", " >>> make_powers_lc(4, range(10))\n", " [[0, 0, 0, 0],\n", " [1, 1, 1, 1],\n", " [2, 4, 8, 16],\n", " [3, 9, 27, 81],\n", " [4, 16, 64, 256],\n", " [5, 25, 125, 625],\n", " [6, 36, 216, 1296],\n", " [7, 49, 343, 2401],\n", " [8, 64, 512, 4096],\n", " [9, 81, 729, 6561]]\n", " \"\"\"" ] }, { "cell_type": "code", "execution_count": null, "id": "8eb3928f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 7, "id": "1caa3d73", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1]]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 1) start with shape: \n", "# Output will be a list of list of ints, \n", "# so create simplest version of that\n", "def make_powers_lc(n, inputs):\n", " return [[1]]\n", "make_powers_lc(4, range(10))" ] }, { "cell_type": "code", "execution_count": 8, "id": "cc3309a8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 2) expand outer list comprehension to have correct number of elements\n", "# using inputs as foundation, we haven't modified the first term yet\n", "def make_powers_lc(n, inputs):\n", " return [[1] for x in inputs]\n", "make_powers_lc(4, range(10))" ] }, { "cell_type": "code", "execution_count": 9, "id": "8a531ebc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1, 1, 1, 1],\n", " [1, 1, 1, 1],\n", " [1, 1, 1, 1],\n", " [1, 1, 1, 1],\n", " [1, 1, 1, 1],\n", " [1, 1, 1, 1],\n", " [1, 1, 1, 1],\n", " [1, 1, 1, 1],\n", " [1, 1, 1, 1],\n", " [1, 1, 1, 1]]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 3) Now expand inner list to have correct number of elements.\n", "def make_powers_lc(n, inputs):\n", " return [[1 for y in range(n)] for x in inputs]\n", "make_powers_lc(4, range(10))" ] }, { "cell_type": "code", "execution_count": 10, "id": "80135012", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1, 0, 0, 0],\n", " [1, 1, 1, 1],\n", " [1, 2, 4, 8],\n", " [1, 3, 9, 27],\n", " [1, 4, 16, 64],\n", " [1, 5, 25, 125],\n", " [1, 6, 36, 216],\n", " [1, 7, 49, 343],\n", " [1, 8, 64, 512],\n", " [1, 9, 81, 729]]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 4) Fix initial term to do calculation\n", "def make_powers_lc(n, inputs):\n", " return [[x**y for y in range(n)] for x in inputs]\n", "make_powers_lc(4, range(10))" ] }, { "cell_type": "code", "execution_count": 11, "id": "513b22ba", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[0, 0, 0, 0],\n", " [1, 1, 1, 1],\n", " [2, 4, 8, 16],\n", " [3, 9, 27, 81],\n", " [4, 16, 64, 256],\n", " [5, 25, 125, 625],\n", " [6, 36, 216, 1296],\n", " [7, 49, 343, 2401],\n", " [8, 64, 512, 4096],\n", " [9, 81, 729, 6561]]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 5) With correct shape, make modifications to ranges/etc. as needed\n", "def make_powers_lc(n, inputs):\n", " return [[x**y for y in range(1, n+1)] for x in inputs]\n", "make_powers_lc(4, range(10))" ] }, { "cell_type": "code", "execution_count": null, "id": "15faca48", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.15" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 5 }