From 97446ff1b4e10c525170210470812065a809af8c Mon Sep 17 00:00:00 2001 From: pszewach Date: Tue, 22 Oct 2019 16:17:32 +0200 Subject: [PATCH 1/2] Lab Completed --- your-code/main.ipynb | 235 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 210 insertions(+), 25 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 7afceca..a8cea76 100755 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -29,9 +29,20 @@ "cell_type": "code", "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]\n" + ] + } + ], "source": [ - "# your code here " + "# your code here\n", + "\n", + "newlist=[i for i in range (1,51)]\n", + "print(newlist)" ] }, { @@ -45,9 +56,18 @@ "cell_type": "code", "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200]\n" + ] + } + ], "source": [ - "# your code here " + "even_n=[i for i in range (2,201) if i % 2 == 0]\n", + "print(even_n)" ] }, { @@ -72,16 +92,27 @@ " [0.71725408, 0.87702738, 0.31244595, 0.76615487],\n", " [0.20754036, 0.57871812, 0.07214068, 0.40356048],\n", " [0.12149553, 0.53222417, 0.9976855 , 0.12536346],\n", - " [0.80930099, 0.50962849, 0.94555126, 0.33364763]];" + " [0.80930099, 0.50962849, 0.94555126, 0.33364763]]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.84062117, 0.48006452, 0.7876326, 0.77109654, 0.44409793, 0.09014516, 0.81835917, 0.87645456, 0.7066597, 0.09610873, 0.41247947, 0.57433389, 0.29960807, 0.42315023, 0.34452557, 0.4751035, 0.17003563, 0.46843998, 0.92796258, 0.69814654, 0.41290051, 0.19561071, 0.16284783, 0.97016248, 0.71725408, 0.87702738, 0.31244595, 0.76615487, 0.20754036, 0.57871812, 0.07214068, 0.40356048, 0.12149553, 0.53222417, 0.9976855, 0.12536346, 0.80930099, 0.50962849, 0.94555126, 0.33364763]\n" + ] + } + ], "source": [ - "# your code here " + "# your code here\n", + "\n", + "list2=[j for i in a for j in i]\n", + "print(list2)" ] }, { @@ -95,9 +126,19 @@ "cell_type": "code", "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.84062117, 0.7876326, 0.77109654, 0.81835917, 0.87645456, 0.7066597, 0.57433389, 0.92796258, 0.69814654, 0.97016248, 0.71725408, 0.87702738, 0.76615487, 0.57871812, 0.53222417, 0.9976855, 0.80930099, 0.50962849, 0.94555126]\n" + ] + } + ], "source": [ - "# your code here " + "# your code here\n", + "list3=[j for i in a for j in i if j>=0.5]\n", + "print(list3)" ] }, { @@ -133,9 +174,21 @@ "cell_type": "code", "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.55867166, 0.06210792, 0.08147297, 0.82579068, 0.91512478, 0.06833034, 0.05440634, 0.65857693, 0.30296619, 0.06769833, 0.96031863, 0.51293743, 0.09143215, 0.71893382, 0.45850679, 0.58256464, 0.59005654, 0.56266457, 0.71600294, 0.87392666, 0.11434044, 0.8694668, 0.65669313, 0.10708681, 0.07529684, 0.46470767, 0.47984544, 0.65368638, 0.14901286, 0.23760688]\n" + ] + } + ], "source": [ - "# your code here " + "# your code here\n", + "\n", + "list4=[k for i in b for j in i for k in j]\n", + "print(list4)\n", + "\n" ] }, { @@ -149,9 +202,20 @@ "cell_type": "code", "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.06210792, 0.08147297, 0.06833034, 0.05440634, 0.30296619, 0.06769833, 0.09143215, 0.45850679, 0.11434044, 0.10708681, 0.07529684, 0.46470767, 0.47984544, 0.14901286, 0.23760688]\n" + ] + } + ], "source": [ - "# your code here " + "# your code here\n", + "\n", + "list5=[k for i in b for j in i for k in j if k<=0.5]\n", + "print(list5)" ] }, { @@ -165,9 +229,24 @@ "cell_type": "code", "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "FileNotFoundError", + "evalue": "[Errno 2] No such file or directory: 'Users/p/Documents/GitHub/lab-list-comprehensions/data'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mpath\u001b[0m \u001b[0;34m=\u001b[0m\u001b[0;34m\"Users/p/Documents/GitHub/lab-list-comprehensions/data\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mcvs_files\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mfilename\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mfilename\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlistdir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mfilename\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mendswith\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'.csv'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'Users/p/Documents/GitHub/lab-list-comprehensions/data'" + ] + } + ], "source": [ - "# your code here " + "# your code here\n", + "path =\"Users/p/Documents/GitHub/lab-list-comprehensions/data\" \n", + "\n", + "cvs_files=[filename for filename in os.listdir(path) if filename.endswith('.csv')]" ] }, { @@ -196,7 +275,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -206,11 +285,22 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'bananas': 2, 'ice-cream': 1, 'carrots': 5, 'cookies': 9, 'guanciale': 7, 'pasta': 2}\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "dictionary1={fruits[i]:quantities[i] for i in range (len(fruits))}\n", + "shopping_dict=dictionary1\n", + "print(shopping_dict)\n" ] }, { @@ -227,11 +317,99 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m (fruits, quantities*2) for fruits, quantities in dictionary1.items()\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "source": [ + "(fruits, quantities*2) for fruits, quantities in dictionary1.items()" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "double_shopping_dict= dictionary1.update((fruits, quantities*2) for fruits, quantities in dictionary1.items())" + ] + }, + { + "cell_type": "code", + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "lst1 = [1, 2, 3]" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "a, b, c = lst1" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_items([('bananas', 4), ('ice-cream', 2), ('carrots', 10), ('cookies', 18), ('guanciale', 14), ('pasta', 4)])" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dictionary1.items()" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "# your code here\n", + "dict2={fruits:quantities * 2 for fruits,quantities in dictionary1.items()}\n", + "\n", + "#tuple unpacking" ] }, { @@ -248,11 +426,12 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 28, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "cheap_shopping_dict = {fruits:8 if quantities>=10 else quantities for fruits,quantities in dict2.items() }" ] }, { @@ -298,6 +477,7 @@ } ], "metadata": { + "file_extension": ".py", "kernelspec": { "display_name": "Python 3", "language": "python", @@ -313,8 +493,13 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" - } + "version": "3.6.4" + }, + "mimetype": "text/x-python", + "name": "python", + "npconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": 3 }, "nbformat": 4, "nbformat_minor": 2 From 5e1c6eab7e98cc2c41a3447491968c324b3d6597 Mon Sep 17 00:00:00 2001 From: pszewach Date: Tue, 22 Oct 2019 16:19:47 +0200 Subject: [PATCH 2/2] Fixed error --- your-code/main.ipynb | 507 +------------------------------------------ 1 file changed, 1 insertion(+), 506 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index a8cea76..ae1bc28 100755 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -1,506 +1 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# List Comprehensions\n", - "\n", - "Complete the following set of exercises to solidify your knowledge of list comprehensions." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "import os;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 1. Use a list comprehension to create and print a list of consecutive integers starting with 1 and ending with 50." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]\n" - ] - } - ], - "source": [ - "# your code here\n", - "\n", - "newlist=[i for i in range (1,51)]\n", - "print(newlist)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 2. Use a list comprehension to create and print a list of even numbers starting with 2 and ending with 200." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200]\n" - ] - } - ], - "source": [ - "even_n=[i for i in range (2,201) if i % 2 == 0]\n", - "print(even_n)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 3. Use a list comprehension to create and print a list containing all elements of the 10 x 4 array below." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [], - "source": [ - "a = [[0.84062117, 0.48006452, 0.7876326 , 0.77109654],\n", - " [0.44409793, 0.09014516, 0.81835917, 0.87645456],\n", - " [0.7066597 , 0.09610873, 0.41247947, 0.57433389],\n", - " [0.29960807, 0.42315023, 0.34452557, 0.4751035 ],\n", - " [0.17003563, 0.46843998, 0.92796258, 0.69814654],\n", - " [0.41290051, 0.19561071, 0.16284783, 0.97016248],\n", - " [0.71725408, 0.87702738, 0.31244595, 0.76615487],\n", - " [0.20754036, 0.57871812, 0.07214068, 0.40356048],\n", - " [0.12149553, 0.53222417, 0.9976855 , 0.12536346],\n", - " [0.80930099, 0.50962849, 0.94555126, 0.33364763]]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0.84062117, 0.48006452, 0.7876326, 0.77109654, 0.44409793, 0.09014516, 0.81835917, 0.87645456, 0.7066597, 0.09610873, 0.41247947, 0.57433389, 0.29960807, 0.42315023, 0.34452557, 0.4751035, 0.17003563, 0.46843998, 0.92796258, 0.69814654, 0.41290051, 0.19561071, 0.16284783, 0.97016248, 0.71725408, 0.87702738, 0.31244595, 0.76615487, 0.20754036, 0.57871812, 0.07214068, 0.40356048, 0.12149553, 0.53222417, 0.9976855, 0.12536346, 0.80930099, 0.50962849, 0.94555126, 0.33364763]\n" - ] - } - ], - "source": [ - "# your code here\n", - "\n", - "list2=[j for i in a for j in i]\n", - "print(list2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 4. Add a condition to the list comprehension above so that only values greater than or equal to 0.5 are printed." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0.84062117, 0.7876326, 0.77109654, 0.81835917, 0.87645456, 0.7066597, 0.57433389, 0.92796258, 0.69814654, 0.97016248, 0.71725408, 0.87702738, 0.76615487, 0.57871812, 0.53222417, 0.9976855, 0.80930099, 0.50962849, 0.94555126]\n" - ] - } - ], - "source": [ - "# your code here\n", - "list3=[j for i in a for j in i if j>=0.5]\n", - "print(list3)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 5. Use a list comprehension to create and print a list containing all elements of the 5 x 2 x 3 array below." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [], - "source": [ - "b = [[[0.55867166, 0.06210792, 0.08147297],\n", - " [0.82579068, 0.91512478, 0.06833034]],\n", - "\n", - " [[0.05440634, 0.65857693, 0.30296619],\n", - " [0.06769833, 0.96031863, 0.51293743]],\n", - "\n", - " [[0.09143215, 0.71893382, 0.45850679],\n", - " [0.58256464, 0.59005654, 0.56266457]],\n", - "\n", - " [[0.71600294, 0.87392666, 0.11434044],\n", - " [0.8694668 , 0.65669313, 0.10708681]],\n", - "\n", - " [[0.07529684, 0.46470767, 0.47984544],\n", - " [0.65368638, 0.14901286, 0.23760688]]];" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0.55867166, 0.06210792, 0.08147297, 0.82579068, 0.91512478, 0.06833034, 0.05440634, 0.65857693, 0.30296619, 0.06769833, 0.96031863, 0.51293743, 0.09143215, 0.71893382, 0.45850679, 0.58256464, 0.59005654, 0.56266457, 0.71600294, 0.87392666, 0.11434044, 0.8694668, 0.65669313, 0.10708681, 0.07529684, 0.46470767, 0.47984544, 0.65368638, 0.14901286, 0.23760688]\n" - ] - } - ], - "source": [ - "# your code here\n", - "\n", - "list4=[k for i in b for j in i for k in j]\n", - "print(list4)\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 6. Add a condition to the list comprehension above so that the last value in each subarray is printed, but only if it is less than or equal to 0.5." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0.06210792, 0.08147297, 0.06833034, 0.05440634, 0.30296619, 0.06769833, 0.09143215, 0.45850679, 0.11434044, 0.10708681, 0.07529684, 0.46470767, 0.47984544, 0.14901286, 0.23760688]\n" - ] - } - ], - "source": [ - "# your code here\n", - "\n", - "list5=[k for i in b for j in i for k in j if k<=0.5]\n", - "print(list5)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 7. Use a list comprehension to select and print the names of all CSV files in the */data* directory." - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "ename": "FileNotFoundError", - "evalue": "[Errno 2] No such file or directory: 'Users/p/Documents/GitHub/lab-list-comprehensions/data'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mpath\u001b[0m \u001b[0;34m=\u001b[0m\u001b[0;34m\"Users/p/Documents/GitHub/lab-list-comprehensions/data\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mcvs_files\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mfilename\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mfilename\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlistdir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mfilename\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mendswith\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'.csv'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'Users/p/Documents/GitHub/lab-list-comprehensions/data'" - ] - } - ], - "source": [ - "# your code here\n", - "path =\"Users/p/Documents/GitHub/lab-list-comprehensions/data\" \n", - "\n", - "cvs_files=[filename for filename in os.listdir(path) if filename.endswith('.csv')]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Bonus: Dictionary Comprehensions\n", - "\n", - "The concept of *comprehension* is not unique for *lists* in Python. You can also create a **dictionary comprehension** using the following syntax:\n", - "```python\n", - "{key: value for key, value in iterable}\n", - "```\n", - "Remember that each element of the iterable must be a key-value pair. \n", - "\n", - "We use dict comprehensions to create dictionaries using a single easy-readable line of code. \n", - "\n", - "Now, imagine that your friends are coming over to your house for dinner and you want to buy the ingredients of your secret recipe. \n", - "\n", - "**Use a dictionary comprehension to create your own \"shopping dictionary\". This dictionary must include the items you want to buy as keys and the quantity as values. Store the resulting dictionary in a variable called `shopping_dict`.**\n", - "Your output dictionary should look like this:\n", - "```python\n", - "{'Bananas': 2, 'Ice-cream': 1, 'Carrots': 5, 'Cookies': 9, 'Guanciale': 7, 'Pasta': 2}\n", - "```\n", - "**Hint:** Beware keys should be capitalized. You can use the lists below and function [zip](https://www.w3schools.com/python/ref_func_zip.asp) to create the iteration." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [], - "source": [ - "fruits = ['bananas', 'ice-cream', 'carrots', 'cookies', 'guanciale', 'pasta']\n", - "quantities = [2, 1, 5, 9, 7, 2]" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'bananas': 2, 'ice-cream': 1, 'carrots': 5, 'cookies': 9, 'guanciale': 7, 'pasta': 2}\n" - ] - } - ], - "source": [ - "# your code here\n", - "dictionary1={fruits[i]:quantities[i] for i in range (len(fruits))}\n", - "shopping_dict=dictionary1\n", - "print(shopping_dict)\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Your mom called and said that your father, your siblings and her are also coming over for dinner, so you'll need double the ingredients you were planning to buy! \n", - "\n", - "**Create a new variable called `double_shopping_dict` using a dict comprehension that includes the new food quantities you need. This time use `shopping_dict` to create the comprehension loop.** Your output dictionary should look like this:\n", - "```python\n", - "{'Bananas': 4, 'Ice-cream': 2, 'Carrots': 10, 'Cookies': 18, 'Guanciale': 14, 'Pasta': 4}\n", - "```" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "invalid syntax (, line 1)", - "output_type": "error", - "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m (fruits, quantities*2) for fruits, quantities in dictionary1.items()\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" - ] - } - ], - "source": [ - "(fruits, quantities*2) for fruits, quantities in dictionary1.items()" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [], - "source": [ - "double_shopping_dict= dictionary1.update((fruits, quantities*2) for fruits, quantities in dictionary1.items())" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [], - "source": [ - "lst1 = [1, 2, 3]" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [], - "source": [ - "a, b, c = lst1" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "c" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "dict_items([('bananas', 4), ('ice-cream', 2), ('carrots', 10), ('cookies', 18), ('guanciale', 14), ('pasta', 4)])" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dictionary1.items()" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here\n", - "dict2={fruits:quantities * 2 for fruits,quantities in dictionary1.items()}\n", - "\n", - "#tuple unpacking" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "After checking the shopping dictionary, you realise that you don't have enough money to buy that many ingredients. As a solution you decide that if you need more than 10 units of a specific food, you will just buy 8 of them. \n", - "\n", - "**Create a new variable called `cheap_shopping_dict` using a dict comprehension that includes the new food quantities you need.** Your output dictionary should look like this:\n", - "```python\n", - "{'Bananas': 4, 'Ice-cream': 2, 'Carrots': 10, 'Cookies': 8, 'Guanciale': 8, 'Pasta': 4}\n", - "```" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here\n", - "cheap_shopping_dict = {fruits:8 if quantities>=10 else quantities for fruits,quantities in dict2.items() }" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now, hurry and go buy everything!" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Bonus" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Try to solve these katas using list comprehensions." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Easy**\n", - "- [Insert values](https://www.codewars.com/kata/invert-values)\n", - "- [Sum Square(n)](https://www.codewars.com/kata/square-n-sum)\n", - "- [Digitize](https://www.codewars.com/kata/digitize)\n", - "- [List filtering](https://www.codewars.com/kata/list-filtering)\n", - "- [Arithmetic list](https://www.codewars.com/kata/541da001259d9ca85d000688)\n", - "\n", - "**Medium**\n", - "- [Multiples of 3 or 5](https://www.codewars.com/kata/514b92a657cdc65150000006)\n", - "- [Count of positives / sum of negatives](https://www.codewars.com/kata/count-of-positives-slash-sum-of-negatives)\n", - "- [Categorize new member](https://www.codewars.com/kata/5502c9e7b3216ec63c0001aa)\n", - "\n", - "**Advanced**\n", - "- [Queue time counter](https://www.codewars.com/kata/queue-time-counter)" - ] - } - ], - "metadata": { - "file_extension": ".py", - "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.4" - }, - "mimetype": "text/x-python", - "name": "python", - "npconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": 3 - }, - "nbformat": 4, - "nbformat_minor": 2 -} +{"cells":[{"cell_type":"markdown","metadata":{},"source":["# List Comprehensions\n","\n","Complete the following set of exercises to solidify your knowledge of list comprehensions."]},{"cell_type":"code","execution_count":2,"metadata":{},"outputs":[],"source":"import os;"},{"cell_type":"markdown","metadata":{},"source":"#### 1. Use a list comprehension to create and print a list of consecutive integers starting with 1 and ending with 50."},{"cell_type":"code","execution_count":3,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]\n"}],"source":"# your code here\n\nnewlist=[i for i in range (1,51)]\nprint(newlist)"},{"cell_type":"markdown","metadata":{},"source":"#### 2. Use a list comprehension to create and print a list of even numbers starting with 2 and ending with 200."},{"cell_type":"code","execution_count":4,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200]\n"}],"source":"even_n=[i for i in range (2,201) if i % 2 == 0]\nprint(even_n)"},{"cell_type":"markdown","metadata":{},"source":"#### 3. Use a list comprehension to create and print a list containing all elements of the 10 x 4 array below."},{"cell_type":"code","execution_count":5,"metadata":{},"outputs":[],"source":"a = [[0.84062117, 0.48006452, 0.7876326 , 0.77109654],\n [0.44409793, 0.09014516, 0.81835917, 0.87645456],\n [0.7066597 , 0.09610873, 0.41247947, 0.57433389],\n [0.29960807, 0.42315023, 0.34452557, 0.4751035 ],\n [0.17003563, 0.46843998, 0.92796258, 0.69814654],\n [0.41290051, 0.19561071, 0.16284783, 0.97016248],\n [0.71725408, 0.87702738, 0.31244595, 0.76615487],\n [0.20754036, 0.57871812, 0.07214068, 0.40356048],\n [0.12149553, 0.53222417, 0.9976855 , 0.12536346],\n [0.80930099, 0.50962849, 0.94555126, 0.33364763]]"},{"cell_type":"code","execution_count":6,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[0.84062117, 0.48006452, 0.7876326, 0.77109654, 0.44409793, 0.09014516, 0.81835917, 0.87645456, 0.7066597, 0.09610873, 0.41247947, 0.57433389, 0.29960807, 0.42315023, 0.34452557, 0.4751035, 0.17003563, 0.46843998, 0.92796258, 0.69814654, 0.41290051, 0.19561071, 0.16284783, 0.97016248, 0.71725408, 0.87702738, 0.31244595, 0.76615487, 0.20754036, 0.57871812, 0.07214068, 0.40356048, 0.12149553, 0.53222417, 0.9976855, 0.12536346, 0.80930099, 0.50962849, 0.94555126, 0.33364763]\n"}],"source":"# your code here\n\nlist2=[j for i in a for j in i]\nprint(list2)"},{"cell_type":"markdown","metadata":{},"source":"#### 4. Add a condition to the list comprehension above so that only values greater than or equal to 0.5 are printed."},{"cell_type":"code","execution_count":7,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[0.84062117, 0.7876326, 0.77109654, 0.81835917, 0.87645456, 0.7066597, 0.57433389, 0.92796258, 0.69814654, 0.97016248, 0.71725408, 0.87702738, 0.76615487, 0.57871812, 0.53222417, 0.9976855, 0.80930099, 0.50962849, 0.94555126]\n"}],"source":"# your code here\nlist3=[j for i in a for j in i if j>=0.5]\nprint(list3)"},{"cell_type":"markdown","metadata":{},"source":"#### 5. Use a list comprehension to create and print a list containing all elements of the 5 x 2 x 3 array below."},{"cell_type":"code","execution_count":8,"metadata":{},"outputs":[],"source":"b = [[[0.55867166, 0.06210792, 0.08147297],\n [0.82579068, 0.91512478, 0.06833034]],\n\n [[0.05440634, 0.65857693, 0.30296619],\n [0.06769833, 0.96031863, 0.51293743]],\n\n [[0.09143215, 0.71893382, 0.45850679],\n [0.58256464, 0.59005654, 0.56266457]],\n\n [[0.71600294, 0.87392666, 0.11434044],\n [0.8694668 , 0.65669313, 0.10708681]],\n\n [[0.07529684, 0.46470767, 0.47984544],\n [0.65368638, 0.14901286, 0.23760688]]];"},{"cell_type":"code","execution_count":9,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[0.55867166, 0.06210792, 0.08147297, 0.82579068, 0.91512478, 0.06833034, 0.05440634, 0.65857693, 0.30296619, 0.06769833, 0.96031863, 0.51293743, 0.09143215, 0.71893382, 0.45850679, 0.58256464, 0.59005654, 0.56266457, 0.71600294, 0.87392666, 0.11434044, 0.8694668, 0.65669313, 0.10708681, 0.07529684, 0.46470767, 0.47984544, 0.65368638, 0.14901286, 0.23760688]\n"}],"source":"# your code here\n\nlist4=[k for i in b for j in i for k in j]\nprint(list4)\n\n"},{"cell_type":"markdown","metadata":{},"source":"#### 6. Add a condition to the list comprehension above so that the last value in each subarray is printed, but only if it is less than or equal to 0.5."},{"cell_type":"code","execution_count":10,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[0.06210792, 0.08147297, 0.06833034, 0.05440634, 0.30296619, 0.06769833, 0.09143215, 0.45850679, 0.11434044, 0.10708681, 0.07529684, 0.46470767, 0.47984544, 0.14901286, 0.23760688]\n"}],"source":"# your code here\n\nlist5=[k for i in b for j in i for k in j if k<=0.5]\nprint(list5)"},{"cell_type":"markdown","metadata":{},"source":"#### 7. Use a list comprehension to select and print the names of all CSV files in the */data* directory."},{"cell_type":"code","execution_count":26,"metadata":{},"outputs":[],"source":"# your code here\npath =\"/Users/p/Documents/Ironhack/Labs/lab-list-comprehensions/data\" \n\ncvs_files=[filename for filename in os.listdir(path) if filename.endswith('.csv')]"},{"cell_type":"markdown","metadata":{},"source":"### Bonus: Dictionary Comprehensions\n\nThe concept of *comprehension* is not unique for *lists* in Python. You can also create a **dictionary comprehension** using the following syntax:\n```python\n{key: value for key, value in iterable}\n```\nRemember that each element of the iterable must be a key-value pair. \n\nWe use dict comprehensions to create dictionaries using a single easy-readable line of code. \n\nNow, imagine that your friends are coming over to your house for dinner and you want to buy the ingredients of your secret recipe. \n\n**Use a dictionary comprehension to create your own \"shopping dictionary\". This dictionary must include the items you want to buy as keys and the quantity as values. Store the resulting dictionary in a variable called `shopping_dict`.**\nYour output dictionary should look like this:\n```python\n{'Bananas': 2, 'Ice-cream': 1, 'Carrots': 5, 'Cookies': 9, 'Guanciale': 7, 'Pasta': 2}\n```\n**Hint:** Beware keys should be capitalized. You can use the lists below and function [zip](https://www.w3schools.com/python/ref_func_zip.asp) to create the iteration."},{"cell_type":"code","execution_count":12,"metadata":{},"outputs":[],"source":"fruits = ['bananas', 'ice-cream', 'carrots', 'cookies', 'guanciale', 'pasta']\nquantities = [2, 1, 5, 9, 7, 2]"},{"cell_type":"code","execution_count":13,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"{'bananas': 2, 'ice-cream': 1, 'carrots': 5, 'cookies': 9, 'guanciale': 7, 'pasta': 2}\n"}],"source":"# your code here\ndictionary1={fruits[i]:quantities[i] for i in range (len(fruits))}\nshopping_dict=dictionary1\nprint(shopping_dict)\n"},{"cell_type":"markdown","metadata":{},"source":"Your mom called and said that your father, your siblings and her are also coming over for dinner, so you'll need double the ingredients you were planning to buy! \n\n**Create a new variable called `double_shopping_dict` using a dict comprehension that includes the new food quantities you need. This time use `shopping_dict` to create the comprehension loop.** Your output dictionary should look like this:\n```python\n{'Bananas': 4, 'Ice-cream': 2, 'Carrots': 10, 'Cookies': 18, 'Guanciale': 14, 'Pasta': 4}\n```"},{"cell_type":"code","execution_count":14,"metadata":{},"outputs":[{"data":{"text/plain":"{('bananas', 4),\n ('carrots', 10),\n ('cookies', 18),\n ('guanciale', 14),\n ('ice-cream', 2),\n ('pasta', 4)}"},"execution_count":14,"metadata":{},"output_type":"execute_result"}],"source":"{(fruits, quantities*2) for fruits, quantities in dictionary1.items()}"},{"cell_type":"code","execution_count":15,"metadata":{},"outputs":[],"source":"double_shopping_dict= dictionary1.update((fruits, quantities*2) for fruits, quantities in dictionary1.items())"},{"cell_type":"code","execution_count":16,"metadata":{},"outputs":[],"source":"lst1 = [1, 2, 3]"},{"cell_type":"code","execution_count":17,"metadata":{},"outputs":[],"source":"a, b, c = lst1"},{"cell_type":"code","execution_count":18,"metadata":{},"outputs":[{"data":{"text/plain":"3"},"execution_count":18,"metadata":{},"output_type":"execute_result"}],"source":"c"},{"cell_type":"code","execution_count":19,"metadata":{},"outputs":[{"data":{"text/plain":"dict_items([('bananas', 4), ('ice-cream', 2), ('carrots', 10), ('cookies', 18), ('guanciale', 14), ('pasta', 4)])"},"execution_count":19,"metadata":{},"output_type":"execute_result"}],"source":"dictionary1.items()"},{"cell_type":"code","execution_count":20,"metadata":{},"outputs":[],"source":"# your code here\ndict2={fruits:quantities * 2 for fruits,quantities in dictionary1.items()}\n\n#tuple unpacking"},{"cell_type":"markdown","metadata":{},"source":"After checking the shopping dictionary, you realise that you don't have enough money to buy that many ingredients. As a solution you decide that if you need more than 10 units of a specific food, you will just buy 8 of them. \n\n**Create a new variable called `cheap_shopping_dict` using a dict comprehension that includes the new food quantities you need.** Your output dictionary should look like this:\n```python\n{'Bananas': 4, 'Ice-cream': 2, 'Carrots': 10, 'Cookies': 8, 'Guanciale': 8, 'Pasta': 4}\n```"},{"cell_type":"code","execution_count":25,"metadata":{},"outputs":[{"ename":"SyntaxError","evalue":"invalid syntax (, line 2)","output_type":"error","traceback":["\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m cheap_shopping_dict = {fruits:8 if quantities>=10 else quantities fruits,quantities in dict2.items()}\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"]}],"source":"# your code here\ncheap_shopping_dict = {fruits:8 if quantities>=10 else quantities fruits,quantities in dict2.items()}\n# cheap_shopping_dict={fruits:quantities if quantities>=10,quantities in dict2.items()}"},{"cell_type":"markdown","metadata":{},"source":"Now, hurry and go buy everything!"},{"cell_type":"markdown","metadata":{},"source":["### Bonus"]},{"cell_type":"markdown","metadata":{},"source":["Try to solve these katas using list comprehensions."]},{"cell_type":"markdown","metadata":{},"source":["**Easy**\n","- [Insert values](https://www.codewars.com/kata/invert-values)\n","- [Sum Square(n)](https://www.codewars.com/kata/square-n-sum)\n","- [Digitize](https://www.codewars.com/kata/digitize)\n","- [List filtering](https://www.codewars.com/kata/list-filtering)\n","- [Arithmetic list](https://www.codewars.com/kata/541da001259d9ca85d000688)\n","\n","**Medium**\n","- [Multiples of 3 or 5](https://www.codewars.com/kata/514b92a657cdc65150000006)\n","- [Count of positives / sum of negatives](https://www.codewars.com/kata/count-of-positives-slash-sum-of-negatives)\n","- [Categorize new member](https://www.codewars.com/kata/5502c9e7b3216ec63c0001aa)\n","\n","**Advanced**\n","- [Queue time counter](https://www.codewars.com/kata/queue-time-counter)"]}],"nbformat":4,"nbformat_minor":2,"metadata":{"language_info":{"name":"python","codemirror_mode":{"name":"ipython","version":3}},"orig_nbformat":2,"file_extension":".py","mimetype":"text/x-python","name":"python","npconvert_exporter":"python","pygments_lexer":"ipython3","version":3}} \ No newline at end of file