{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# List comprehension practice\n", "## 1. Stemming words" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Consider the following list of activities." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "activities = ['running', 'jumping', 'shouting', 'dancing', 'singing', 'eating', 'reading']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this exercise, we will:\n", "- remove the `ing` from the end of every activity\n", "- add a dash `-` to each activity\n", "- sort the result\n", "- join the activities into a single string using the delimiter `|`\n", "\n", "To start, I have added a simple comprehension that just copies the list. \n", "- `for a in activities`, it returns `a`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# The following list comprehension just returns every activity in the list of activities\n", "[a for a in activities]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Modify the comprehension so it returns every activity with the last three letters removed. You'll want to get: \n", "\n", " ['runn', 'jump', 'shout', 'danc', 'sing', 'eat', 'read']" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Copy comprehension above, modify to remove last three letters\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Answer:** [a[:-3] for a in activities]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Now, add a dash after each element in the list to get:\n", "\n", " ['runn-', 'jump-', 'shout-', 'danc-', 'sing-', 'eat-', 'read-'] " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Copy comprehension above, modify to add a dash \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Answer:** [a[:-3]+\"-\" for a in activities]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. Now, sort the list to get:\n", "\n", " ['danc-', 'eat-', 'jump-', 'read-', 'runn-', 'shout-', 'sing-']" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Copy comprehension above, sort it\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Answer:** sorted([a[:-3]+\"-\" for a in activities])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. Now, merge the list back into a string to get:\n", "\n", " 'danc- | eat- | jump- | read- | runn- | shout- | sing-'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Copy comprehension above, join with a pipe character '|'\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Answer:** ' | '.join(sorted([a[:-3]+\"-\" for a in activities]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Filtering lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following exercise is taken from a short poem, \"The Fourth\", written by Shel Silverstein." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fourth = [\"Oh\", \"CRASH!\", \"my\", \"BASH!\", \"it’s\", \"BANG!\", \"the\", \"ZANG!\", \"Fourth\", \"WHOOSH!\", \"of\", \"BAROOM!\", \"July\", \"WHEW!\"]\n", "print(\"\\n\".join(fourth)) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's write a list comprehension to:\n", "- Remove all of the firework sounds\n", "- Keep only firework sounds, without exclamation points and in lower case\n", "\n", "To start, I have added a simple comprehension that just copies all the words in the list." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# The following comprehension lists all the words in the fourth of July poem\n", "[w for w in fourth]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Modify the list comprehension to list only words without an exclamation point. \n", "You will need to use an `if` filter. The result should be:\n", "\n", " ['Oh', 'my', 'it’s', 'the', 'Fourth', 'of', 'July']" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Modify the list comprehension above, to list only words without an exclamation point\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Answer:** [w for w in fourth if \"!\" not in w]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Join the result into a single sentence, using spaces as the join character. The result should be:\n", " \n", " 'Oh my it’s the Fourth of July'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Modify the list comprehension above, joining on spaces to create a single message\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Answer:** \" \".join([w for w in fourth if \"!\" not in w])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, write a list comprehension to list only words that **do** include an exclamation point. The answer should be:\n", " \n", " ['CRASH!', 'BASH!', 'BANG!', 'ZANG!', 'WHOOSH!', 'BAROOM!', 'WHEW!']" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Write a list comprehension to list only words that include an exclamation point\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Answer:** [w for w in fourth if \"!\" in w]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, edit the list comprehension above to ensure the words are all lowercase, to get:\n", "\n", " ['crash!', 'bash!', 'bang!', 'zang!', 'whoosh!', 'baroom!', 'whew!']" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Modify the list comprehension above, to make the words lowercase\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Answer:** [w.lower() for w in fourth if \"!\" in w]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, edit the comprehension above to remove the exclamation point. You should end with: \n", "\n", " ['crash', 'bash', 'bang', 'zang', 'whoosh', 'baroom', 'whew']" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# And remove the exclamation point\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Answer:** [w.lower()[:-1] for w in fourth if \"!\" in w]" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.6" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "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": true } }, "nbformat": 4, "nbformat_minor": 2 }