diff --git a/List comprehension.ipynb b/List comprehension.ipynb index 88464fd..1164d3f 100644 --- a/List comprehension.ipynb +++ b/List comprehension.ipynb @@ -38,10 +38,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "id": "4a5fbde9", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['BARCELONA', 'MADRID', 'GIRONA', 'MURCIA']" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Define a list of words\n", "list_of_words = [\"Barcelona\", \"Madrid\", \"Girona\", \"Murcia\"]\n", @@ -68,10 +79,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 74, "id": "02c37a8f", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['BARCELONA', 'MADRID', 'GIRONA', 'MURCIA']" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Define a list of words\n", "list_of_words = [\"Barcelona\", \"Madrid\", \"Girona\", \"Murcia\"]\n", @@ -112,10 +134,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 75, "id": "c9ff4031", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['Barcelona']" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Define a list of words\n", "list_of_words = [\"Barcelona\", \"Madrid\", \"Girona\", \"Murcia\"]\n", @@ -144,10 +177,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 76, "id": "6c8dc69f", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['Barcelona']" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Define a list of words\n", "list_of_words = [\"Barcelona\", \"Madrid\", \"Girona\", \"Murcia\"]\n", @@ -171,12 +215,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 77, "id": "281ba562", "metadata": {}, "outputs": [], "source": [ - "# we compute square as i**2" + "# we compute square as i**2\n" ] }, { @@ -189,11 +233,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 78, "id": "de07f372", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ] + } + ], + "source": [ + "squared1 = []\n", + "for i in range(1, 11):\n", + " squared1.append(i**2)\n", + "print(squared1)" + ] }, { "cell_type": "markdown", @@ -205,11 +262,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 79, "id": "2efdf34e", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ] + } + ], + "source": [ + "squared2 = [i**2 for i in range(1, 11)]\n", + "print(squared2)" + ] }, { "cell_type": "markdown", @@ -224,7 +292,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 80, "id": "cee227e9", "metadata": {}, "outputs": [], @@ -242,11 +310,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 81, "id": "649b9114", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Bercelone', 'Medrid', 'Gerone', 'Murcie']\n" + ] + } + ], + "source": [ + "e_list = []\n", + "for city in list_of_words:\n", + " new_name = \"\"\n", + " for c in city:\n", + " if c == \"a\":\n", + " new_name += \"e\"\n", + " else:\n", + " new_name += c\n", + " e_list.append(new_name)\n", + "print(e_list)" + ] }, { "cell_type": "markdown", @@ -258,11 +345,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 82, "id": "30c4a62a", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Bercelone', 'Medrid', 'Gerone', 'Murcie']\n" + ] + } + ], + "source": [ + "list_of_words = [\"Barcelona\", \"Madrid\", \"Gerona\", \"Murcia\"]\n", + "e_list = [city.replace(\"a\", \"e\") for city in list_of_words]\n", + "print(e_list)" + ] }, { "cell_type": "markdown", @@ -298,10 +397,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 83, "id": "334e2c1a", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['BARCELONA', 'madrid', 'girona', 'murcia']" + ] + }, + "execution_count": 83, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Original list of city names\n", "list_of_words = [\"Barcelona\", \"Madrid\", \"Girona\", \"Murcia\"]\n", @@ -333,12 +443,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 84, "id": "0b42514e", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['BARCELONA', 'madrid', 'girona', 'murcia']" + ] + }, + "execution_count": 84, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "transformed_cities = [i.upper() if len(i) > 6 else i.lower() for i in list_of_words]" + "transformed_cities = [i.upper() if len(i) > 6 else i.lower() for i in list_of_words]\n", + "transformed_cities" ] }, { @@ -387,13 +509,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 85, "id": "de405011", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[[1, 2], [3, 4], [5, 6]]" + ] + }, + "execution_count": 85, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "nested_list = [[1, 2], [3,4], [5, 6]]\n", - "#expected output flat_list = [1, 2, 3, 4, 5, 6]" + "#expected output flat_list = [1, 2, 3, 4, 5, 6]\n", + "nested_list" ] }, { @@ -416,10 +550,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 86, "id": "c0921e98", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5, 6]" + ] + }, + "execution_count": 86, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "new_list = [] # Create an empty list to store the flattened elements.\n", "\n", @@ -427,7 +572,8 @@ " for k in i: # Iterate through the inner list within the outer list.\n", " new_list.append(k) # Append the individual element 'k' to the new_list.\n", "\n", - "# The result will be a flattened list containing all elements from the nested list." + "# The result will be a flattened list containing all elements from the nested list.\n", + "new_list" ] }, { @@ -440,10 +586,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 87, "id": "7b417e31", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5, 6]" + ] + }, + "execution_count": 87, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "flattened_list = [k for i in nested_list for k in i]\n", "flattened_list" @@ -459,7 +616,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 88, "id": "5f90b48b", "metadata": {}, "outputs": [], @@ -469,10 +626,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 89, "id": "cfd2c49e", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4]" + ] + }, + "execution_count": 89, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "new_list = []\n", "\n", @@ -487,10 +655,43 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 90, + "id": "3982ab6a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4]" + ] + }, + "execution_count": 90, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_list2 = [number for array in nested for sub_array in array for sub_sub_array in sub_array for number in sub_sub_array]\n", + "new_list2" + ] + }, + { + "cell_type": "code", + "execution_count": 91, "id": "86fce8b4", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4]" + ] + }, + "execution_count": 91, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "unnesting = [h for i in nested for k in i for j in k for h in j]\n", "unnesting" @@ -534,7 +735,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 92, "id": "33a452d2", "metadata": {}, "outputs": [], @@ -553,22 +754,45 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 93, "id": "4a15be40", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'Clara': '🙃'}" + ] + }, + "execution_count": 93, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "dict_ = {\n", " \"Clara\": \"🙃\"\n", - "}" + "}\n", + "dict_" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 94, "id": "8cb35950", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'Clara': '🙃', 'Albert': '☝️', 'Laura': '🫂'}" + ] + }, + "execution_count": 94, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "dict_ = {} # Create an empty dictionary to store the key-value pairs.\n", "\n", @@ -576,6 +800,7 @@ "for name, emoji in zip(names, emojis):\n", " dict_[name] = emoji # Assign each 'name' as the key and 'emoji' as the value in the dictionary.\n", "\n", + "dict_\n", "# The result will be a dictionary where 'name' maps to 'emoji'." ] }, @@ -591,20 +816,43 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 95, "id": "1cd4fc87", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'Clara': '🙃', 'Albert': '☝️', 'Laura': '🫂'}" + ] + }, + "execution_count": 95, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "name_emoji = {name:emoji for name, emoji in zip(names, emojis)}" + "name_emoji = {name:emoji for name, emoji in zip(names, emojis)}\n", + "name_emoji" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 96, "id": "4b9ad253", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'Clara': '🙃', 'Albert': '☝️', 'Laura': '🫂'}" + ] + }, + "execution_count": 96, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "name_emoji" ] @@ -622,7 +870,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 97, "id": "db0de427", "metadata": {}, "outputs": [], @@ -640,11 +888,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 98, "id": "b8a588fb", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[9, 6, 6, 6]\n" + ] + } + ], + "source": [ + "length_of_words = []\n", + "for city in list_of_words:\n", + " length_of_words.append(len(city))\n", + "print(length_of_words)" + ] }, { "cell_type": "markdown", @@ -656,11 +917,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 99, "id": "15dce979", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "[9, 6, 6, 6]" + ] + }, + "execution_count": 99, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "length_of_words = [len(city) for city in list_of_words]\n", + "length_of_words" + ] }, { "cell_type": "markdown", @@ -679,7 +954,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 100, "id": "a8cf1c56", "metadata": {}, "outputs": [], @@ -717,10 +992,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 101, "id": "a7a6c365", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Positive Keywords: ['excellent', 'love', 'great', 'superb']\n", + "Negative Keywords: ['damaged', 'disappointing', 'terrible']\n" + ] + } + ], "source": [ "# List of feedback comments\n", "feedback_comments = [\n", @@ -734,6 +1018,22 @@ "# List of positive and negative keywords\n", "positive_words = [\"excellent\", \"love\", \"great\", \"superb\"]\n", "negative_words = [\"terrible\", \"disappointing\", \"damaged\"]\n", + "positive_keywords = []\n", + "negative_keywords = []\n", + "\n", + "for comment in feedback_comments:\n", + " for word in comment.split():\n", + " # Remove punctuation from the word\n", + " if word[-1].isalpha() == False:\n", + " word = word[:-1]\n", + "\n", + " if word.lower() in positive_words:\n", + " positive_keywords.append(word)\n", + " elif word.lower() in negative_words:\n", + " negative_keywords.append(word)\n", + "\n", + "print(\"Positive Keywords:\", positive_keywords)\n", + "print(\"Negative Keywords:\", negative_keywords)\n", "\n", "# Continue the code here (example of expected output)\n", "# Positive Keywords: ['excellent', 'love', 'great', 'superb']\n", @@ -792,10 +1092,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 107, "id": "3a778fef", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the service was excellent and i love the product \n", + "the quality of the item was great \n", + "unfortunately the package arrived damaged \n", + "i had a superb experience with your company \n", + "the product was disappointing and terrible \n", + "Positive Keywords: ['excellent', 'love', 'great', 'superb']\n", + "Negative Keywords: ['damaged', 'disappointing', 'terrible']\n" + ] + } + ], "source": [ "# List of feedback comments\n", "feedback_comments = [\n", @@ -810,21 +1124,41 @@ "positive_words = [\"excellent\", \"love\", \"great\", \"superb\"]\n", "negative_words = [\"terrible\", \"disappointing\", \"damaged\"]\n", "\n", + "clear_comments = []\n", + "punktuation_marks = [\".\", \",\", \"!\", \"?\", \";\", \":\"]\n", + "for comment in feedback_comments:\n", + " clear_comment = \"\"\n", + " for word in comment.split():\n", + " # print(word, word[-1])\n", + " if word[-1] in punktuation_marks:\n", + " word = word[:-1]\n", + " clear_comment += word + \" \" \n", + " clear_comments.append(clear_comment.lower()) \n", + "print(*clear_comments, sep=\"\\n\")\n", + "\n", "# Extract positive keywords using list comprehension\n", - "positive_keywords = [word for comment in feedback_comments for word in comment.lower().split() if word in positive_words]\n", + "positive_keywords = [word for comment in clear_comments for word in comment.split() if word in positive_words]\n", "\n", "# Extract negative keywords using list comprehension\n", - "negative_keywords = [word for comment in feedback_comments for word in comment.lower().split() if word in negative_words]\n", + "negative_keywords = [word for comment in clear_comments for word in comment.split() if word in negative_words]\n", "\n", "# Display the extracted keywords\n", "print(\"Positive Keywords:\", positive_keywords)\n", "print(\"Negative Keywords:\", negative_keywords)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a69161a7", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -838,7 +1172,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.1" }, "nbTranslate": { "displayLangs": [