From 81b2cbee87724507ee859a6501872d33990104fd Mon Sep 17 00:00:00 2001 From: Stereo-Alex Date: Thu, 14 Jan 2021 21:02:48 +0100 Subject: [PATCH] final version of the lab --- your-code/challenge-1.ipynb | 170 +++++++++++++++----- your-code/challenge-2.ipynb | 301 +++++++++++++++++++++++++++++------- your-code/challenge-3.ipynb | 121 ++++++++++++--- 3 files changed, 478 insertions(+), 114 deletions(-) diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 48f6b46..eb74f33 100755 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -15,11 +15,11 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "tup = (\"I\",)" ] }, { @@ -33,11 +33,22 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here" + "type(tup) " ] }, { @@ -55,13 +66,27 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "AttributeError", + "evalue": "'tuple' object has no attribute 'append'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtup\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"r\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"o\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"n\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"h\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"a\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"c\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"k\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'" + ] + } + ], "source": [ - "# Your code here\n", "\n", - "# Your explanation here\n" + "\n", + "tup.append(\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + "\n", + "#tupples are inmutable so they will not have an append feature\n" ] }, { @@ -79,13 +104,11 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n", - "\n", - "# Your explanation here\n" + "tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")" ] }, { @@ -103,11 +126,24 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 28, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n')\n", + "('h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here\n" + "tup1 = tup[0:4]\n", + "tup2 = tup[4:]\n", + "\n", + "print(tup1)\n", + "print(tup2)\n" ] }, { @@ -121,11 +157,21 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 30, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here\n" + "tup3 = tup1 + tup2 \n", + "\n", + "print(tup3)\n" ] }, { @@ -137,11 +183,23 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 33, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "4\n", + "8\n" + ] + } + ], "source": [ - "# Your code here\n" + "print(len(tup1))\n", + "print(len(tup2))\n", + "print(len(tup1) + len(tup2))" ] }, { @@ -153,11 +211,19 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 36, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the position of h in tup3 is: 4\n" + ] + } + ], "source": [ - "# Your code here" + "print(\"the position of h in tup3 is: \", tup3.index(\"h\"))\n" ] }, { @@ -177,11 +243,33 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 45, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the letter a is in tup3 True\n", + "the letter b is in tup3 False\n", + "the letter c is in tup3 True\n", + "the letter d is in tup3 False\n", + "the letter e is in tup3 False\n" + ] + } + ], "source": [ - "# Your code here\n" + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "\n", + "#for letter in letters: \n", + "# if letter in tup3:\n", + "# print(letter, \"is in tup3\", \"True\")\n", + "# else:\n", + "# print(letter, \"is not in tup3\", \"False\")\n", + "\n", + "\n", + "for letter in letters:\n", + " print(\"the letter\", letter, \"is in tup3 \", letter in tup3) " ] }, { @@ -195,11 +283,25 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 41, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the letters are ['a', 'c']\n" + ] + } + ], "source": [ - "# Your code here\n" + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "count = []\n", + "for letter in letters: \n", + " if letter in tup3:\n", + " count.append(letter)\n", + "\n", + "print(\"the letters are \", count)" ] }, { @@ -214,9 +316,7 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# Your code here\n" - ] + "source": [] } ], "metadata": { @@ -235,7 +335,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.9.0" } }, "nbformat": 4, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index ef7bde3..a545edc 100755 --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -13,7 +13,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -38,11 +38,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[19, 31, 48, 89, 79, 63, 87, 17, 40, 23, 3, 74, 97, 62, 42, 24, 25, 99, 1, 80, 64, 65, 57, 95, 35, 30, 78, 29, 60, 93, 49, 83, 44, 13, 88, 53, 37, 47, 7, 73, 70, 90, 81, 56, 32, 5, 50, 26, 82, 14, 41, 39, 91, 18, 8, 4, 68, 59, 21, 76, 98, 55, 2, 92, 69, 43, 9, 77, 27, 52, 20, 61, 16, 15, 36, 75, 33, 72, 71, 94]\n" + ] + } + ], + "source": [ + "sample_list_1 = (random.sample(range(1, 100), 80))\n", + "\n", + "print(sample_list_1)" ] }, { @@ -54,11 +64,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "80 Yes because there is replacement when using the variable random.sample()\n" + ] + } + ], + "source": [ + "set1 = set(sample_list_1)\n", + "\n", + "print(len(set1), \"Yes because there is replacement when using the variable random.sample()\")" ] }, { @@ -77,11 +97,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 26, 34, 80, 71, 86, 72, 89, 99, 98, 8, 57, 80, 78, 69, 61, 56, 47, 63, 19, 9, 69, 83, 13, 90, 86, 13, 78, 71, 76, 75, 55, 61, 69, 44, 45, 54, 98, 56, 68, 66, 94, 99, 76, 82, 82, 32, 88, 63, 61, 37, 46, 3, 82, 77, 30, 92, 3, 30, 6, 69, 11, 50, 17, 93, 65, 42, 95, 27, 66, 95, 88, 54, 54, 98, 26, 87, 40, 24, 90]\n" + ] + } + ], "source": [ - "# Your code here\n" + "from random import randrange\n", + "sample_list_2 = []\n", + "for number in range(80):\n", + " sample_list_2.append(randrange(1, 100))\n", + " \n", + "print(sample_list_2)" ] }, { @@ -93,11 +126,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "53 the lenght has changed since sets don't allow for repeats.\n" + ] + } + ], + "source": [ + "set2 = set(sample_list_2)\n", + "\n", + "print(len(set2), \"the lenght has changed since sets don't allow for repeats.\")\n" ] }, { @@ -109,11 +152,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1, 4, 5, 7, 14, 15, 16, 18, 20, 21, 23, 25, 29, 31, 33, 35, 36, 39, 41, 43, 48, 49, 52, 53, 59, 60, 62, 64, 70, 73, 74, 79, 81, 91, 97}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set3 = set()\n", + "for number in set1:\n", + " if number not in set2: \n", + " set3.add(number)\n", + " \n", + "print(set3)" ] }, { @@ -125,11 +181,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{34, 66, 6, 11, 45, 46, 54, 86}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set4 = set()\n", + "for number in set2:\n", + " if number not in set1:\n", + " set4.add(number)\n", + "print(set4)" ] }, { @@ -141,11 +209,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{2, 3, 8, 9, 13, 17, 19, 24, 26, 27, 30, 32, 37, 40, 42, 44, 47, 50, 55, 56, 57, 61, 63, 65, 68, 69, 71, 72, 75, 76, 77, 78, 80, 82, 83, 87, 88, 89, 90, 92, 93, 94, 95, 98, 99}\n" + ] + } + ], "source": [ - "# Your code here\n" + "set5 = set()\n", + "for number in set1:\n", + " if number in set2:\n", + " set5.add(number)\n", + " \n", + "print(set5)" ] }, { @@ -165,11 +246,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "80 this is the maximun length of our sets\n", + "53 this is the maximun lenght of our sets but without any repeats\n", + "35 this are the diferences in between the two, ie numbers that didn't make it into set 2 due to repeats\n", + "8 this are the repeats inside of set 2 \n", + "45 numbers both in set 1 and 2 \n" + ] + } + ], + "source": [ + "print(len(set1),\"this is the maximun length of our sets\")\n", + "print(len(set2),\"this is the maximun lenght of our sets but without any repeats\")\n", + "print(len(set3),\"this are the diferences in between the two, ie numbers that didn't make it into set 2 due to repeats\")\n", + "print(len(set4),\"this are the repeats inside of set 2 \")\n", + "print(len(set5),\"numbers both in set 1 and 2 \")" ] }, { @@ -181,11 +278,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "set6 = set()\n", + "\n" ] }, { @@ -197,11 +295,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1, 2, 3, 4, 5, 7, 8, 9, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 26, 27, 29, 30, 31, 32, 33, 35, 36, 37, 39, 40, 41, 42, 43, 44, 47, 48, 49, 50, 52, 53, 55, 56, 57, 59, 60, 61, 62, 63, 64, 65, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 87, 88, 89, 90, 91, 92, 93, 94, 95, 97, 98, 99}\n" + ] + } + ], "source": [ - "# Your code here\n" + "\n", + "set6.update(set3, set5)\n", + "\n", + "print(set6)" ] }, { @@ -213,11 +322,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "set1 == set6" ] }, { @@ -229,11 +349,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ] + } + ], "source": [ - "# Your code here\n" + "print(set2.issubset(set1))\n", + "print(set3.issubset(set1))" ] }, { @@ -247,11 +377,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "set5.union(set3, set4, set5)\n", + "set1.union(set1, set2)\n", + "\n", + "print(set5 == set1)" ] }, { @@ -263,11 +404,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "element_popped = set1.pop() \n", + "\n", + "print(element_popped)\n" ] }, { @@ -281,14 +432,46 @@ "```" ] }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the remaining elements are {2, 3, 4, 5, 7, 8, 9, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 26, 27, 29, 30, 31, 32, 33, 35, 36, 37, 39, 40, 41, 42, 43, 44, 47, 48, 49, 50, 52, 53, 55, 56, 57, 59, 60, 61, 62, 63, 64, 65, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 87, 88, 89, 90, 91, 92, 93, 94, 95, 97, 99}\n" + ] + } + ], + "source": [ + "list_to_remove = [1, 9, 11, 19, 21, 29, 31, 39, 41, 49, 51, 59, 61, 69, 71, 79, 81, 89, 91, 99]\n", + "\n", + "for number in set1:\n", + " if number not in list_to_remove:\n", + " copy_of_set = set1.copy()\n", + " copy_of_set.remove(number)\n", + " \n", + " \n", + "\n", + " \n", + "print(f\"the remaining elements are {copy_of_set}\")" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# Your code here\n" - ] + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -307,7 +490,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.9.0" } }, "nbformat": 4, diff --git a/your-code/challenge-3.ipynb b/your-code/challenge-3.ipynb index 74d65e0..40c85f8 100755 --- a/your-code/challenge-3.ipynb +++ b/your-code/challenge-3.ipynb @@ -21,11 +21,23 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Penelope': 178, 'Arnau': 172, 'Marta': 175}\n" + ] + } + ], "source": [ - "# Your code here\n" + "dict1 = {\"Penelope\": 178,\n", + " \"Arnau\": 172,\n", + " \"Marta\": 175 }\n", + "\n", + "print(dict1)\n" ] }, { @@ -37,11 +49,23 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['Penelope', 'Arnau', 'Marta'])" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "dict1.keys()\n", + "\n" ] }, { @@ -53,11 +77,20 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Penelope': 178, 'Arnau': 172, 'Marta': 175, 'Alex': 186}\n" + ] + } + ], "source": [ - "# Your code here\n" + "dict1[\"Alex\"] = 186\n", + "print(dict1)\n" ] }, { @@ -69,11 +102,20 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Penelope': 178, 'Arnau': 172, 'Marta': 175}\n" + ] + } + ], "source": [ - "# Your code here\n" + "del dict1[\"Alex\"]\n", + "print(dict1)\n" ] }, { @@ -87,7 +129,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 69, "metadata": {}, "outputs": [], "source": [ @@ -121,11 +163,27 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 74, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'a': 8, 'about': 1, 'all': 1, 'although': 3, 'and': 23, 'are': 1, 'at': 1, 'baby': 14, 'backseat': 1, 'bag': 1, 'bar': 1, 'be': 16, 'bedsheets': 3, 'begin': 1, 'best': 1, 'body': 17, 'boy': 2, 'brand': 6, 'can': 1, 'chance': 1, 'club': 1, 'come': 37, 'conversation': 1, 'crazy': 2, 'dance': 1, 'date': 1, 'day': 6, 'discovering': 6, 'do': 3, 'doing': 2, \"don't\": 2, 'drinking': 1, 'driver': 1, 'eat': 1, 'every': 6, 'falling': 3, 'family': 1, 'fast': 1, 'fill': 2, 'find': 1, 'first': 1, 'follow': 6, 'for': 3, 'friends': 1, 'get': 1, 'girl': 2, 'give': 1, 'go': 2, 'going': 1, 'grab': 2, 'hand': 1, 'handmade': 2, 'heart': 3, 'hours': 2, 'how': 1, 'i': 6, \"i'll\": 1, \"i'm\": 23, 'in': 27, 'is': 5, \"isn't\": 1, 'it': 1, 'jukebox': 1, 'just': 1, 'kiss': 1, 'know': 2, 'last': 3, 'lead': 6, 'leave': 1, 'let': 1, \"let's\": 2, 'like': 10, 'love': 25, 'lover': 1, 'magnet': 3, 'make': 1, 'man': 1, 'may': 2, 'me': 10, 'mind': 2, 'much': 2, 'my': 33, 'new': 6, 'night': 3, 'not': 2, 'now': 11, 'of': 6, 'okay': 1, 'on': 40, 'one': 1, 'our': 1, 'out': 1, 'over': 1, 'place': 1, 'plate': 1, 'play': 1, 'pull': 3, 'push': 3, 'put': 3, 'radio': 1, 'room': 3, 'say': 2, 'shape': 6, 'shots': 1, 'singing': 2, 'slow': 1, 'smell': 3, 'so': 2, 'somebody': 2, 'something': 6, 'sour': 1, 'start': 2, 'stop': 1, 'story': 1, 'sweet': 1, 'table': 1, 'take': 1, 'talk': 4, 'taxi': 1, 'tell': 1, 'that': 2, 'the': 18, 'then': 3, 'thrifty': 1, 'to': 2, 'too': 5, 'trust': 1, 'up': 3, 'van': 1, 'waist': 2, 'want': 2, 'was': 2, 'we': 7, \"we're\": 1, 'week': 1, 'were': 3, 'where': 1, 'with': 22, 'you': 16, 'your': 21}\n" + ] + } + ], "source": [ - "# Your code here\n" + "keys = list(word_freq.keys())\n", + "keys.sort()\n", + "word_freq2 = {}\n", + "\n", + "\n", + "for key in keys:\n", + " word_freq2[key] = word_freq[key]\n", + "print(word_freq2)\n", + " " ] }, { @@ -162,12 +220,35 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 84, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'conversation': [1], \"we're\": [1], 'plate': [1], 'sour': [1], 'jukebox': [1], 'taxi': [1], 'fast': [1], 'bag': [1], 'man': [1], 'going': [1], 'one': [1], 'backseat': [1], 'friends': [1], 'take': [1], 'play': [1], 'okay': [1], 'begin': [1], 'over': [1], 'just': [1], 'are': [1], 'tell': [1], 'drinking': [1], 'our': [1], 'where': [1], \"i'll\": [1], 'all': [1], \"isn't\": [1], 'make': [1], 'lover': [1], 'get': [1], 'radio': [1], 'give': [1], 'can': [1], 'club': [1], 'it': [1], 'out': [1], 'chance': [1], 'first': [1], 'table': [1], 'thrifty': [1], 'driver': [1], 'slow': [1], 'dance': [1], 'trust': [1], 'family': [1], 'week': [1], 'date': [1], 'leave': [1], 'at': [1], 'hand': [1], 'how': [1], 'eat': [1], 'about': [1], 'story': [1], 'sweet': [1], 'best': [1], 'let': [1], 'van': [1], 'shots': [1], 'place': [1], 'find': [1], 'kiss': [1], 'stop': [1], 'bar': [1], \"don't\": [2], 'mind': [2], 'know': [2], 'so': [2], 'start': [2], 'boy': [2], 'girl': [2], 'singing': [2], 'doing': [2], 'somebody': [2], 'handmade': [2], 'may': [2], 'that': [2], 'much': [2], 'grab': [2], 'was': [2], 'say': [2], 'waist': [2], 'want': [2], \"let's\": [2], 'not': [2], 'crazy': [2], 'go': [2], 'to': [2], 'fill': [2], 'hours': [2], 'push': [3], 'then': [3], 'put': [3], 'room': [3], 'magnet': [3], 'up': [3], 'pull': [3], 'last': [3], 'do': [3], 'smell': [3], 'although': [3], 'falling': [3], 'were': [3], 'night': [3], 'heart': [3], 'for': [3], 'bedsheets': [3], 'talk': [4], 'too': [5], 'is': [5], 'every': [6], 'new': [6], 'follow': [6], 'brand': [6], 'of': [6], 'i': [6], 'day': [6], 'lead': [6], 'shape': [6], 'discovering': [6], 'something': [6], 'we': [7], 'a': [8], 'like': [10], 'me': [10], 'now': [11], 'baby': [14], 'you': [16], 'be': [16], 'body': [17], 'the': [18], 'your': [21], 'with': [22], \"i'm\": [23], 'and': [23], 'love': [25], 'in': [27], 'my': [33], 'come': [37], 'on': [40]}\n" + ] + } + ], "source": [ - "# Your code here\n" + "import operator\n", + "sorted_dict = sorted(word_freq.items(), key=operator.itemgetter(1))\n", + "word_freq2 = {}\n", + "\n", + "for (key, value) in sorted_dict:\n", + " # setting the default value as list([])\n", + " # appending the current value\n", + " word_freq2.setdefault(key, []).append(value)\n", + "print(word_freq2)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -186,7 +267,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.9.0" } }, "nbformat": 4,