From d5b75183399c1a6d3d6c6ced08afbab3ada27c70 Mon Sep 17 00:00:00 2001 From: hkzhao Date: Wed, 5 Feb 2020 09:13:32 -0500 Subject: [PATCH] Update 1_list_set_dict.ipynb (#5) * Create 1_list_set_dict.ipynb * Update 1_list_set_dict.ipynb * Update 1_list_set_dict.ipynb * Updated * updated list/set/dict * Updated --- 1_list_set_dict.ipynb | 326 ++++++++++++++++++++++++++++-------------- 1 file changed, 222 insertions(+), 104 deletions(-) diff --git a/1_list_set_dict.ipynb b/1_list_set_dict.ipynb index 99d1f2e..ca17aae 100644 --- a/1_list_set_dict.ipynb +++ b/1_list_set_dict.ipynb @@ -25,54 +25,6 @@ "## A list is a value that contains multiple values in an ordered sequence." ] }, - { - "cell_type": "code", - "metadata": { - "colab_type": "code", - "id": "qn4isYwEqQMK", - "colab": {} - }, - "source": [ - "# list of strings\n", - "['apple', 'cherry', 'banana', 'Lemon', 'Grape']\n", - "# list of mixtures\n", - "[1, 'apple', 'cat', 3.1416]\n", - "# list inside list\n", - "['apple', [1, 2, 3], 'banana']" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "99gRNRrgirez", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# assign a list to a variable\n", - "fruits = ['apple', 'cherry', 'banana', 'Lemon', 'Grape']\n", - "fruits" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "hXWF4nwN5MDq", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# create empty list\n", - "empty_list = []\n", - "empty_list_2 = list()" - ], - "execution_count": 0, - "outputs": [] - }, { "cell_type": "markdown", "metadata": { @@ -1144,51 +1096,6 @@ "execution_count": 0, "outputs": [] }, - { - "cell_type": "markdown", - "metadata": { - "id": "UhTGTkuim2B1", - "colab_type": "text" - }, - "source": [ - "## Zip(list1, list2, ...)\n", - "Combine multipe lists to tuples" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "d1QsVyqamya4", - "colab_type": "code", - "colab": {} - }, - "source": [ - "states = ['NJ', 'NY', 'PA']\n", - "capitals = ['Trenton', 'Albany', 'Harrisburg']\n", - "\n", - "for s, c in zip(states, capitals):\n", - " print('The capital city of %s state is %s' % (s, c))" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "7XIzCbhrogr3", - "colab_type": "code", - "colab": {} - }, - "source": [ - "states = ['NJ', 'NY', 'PA', 'MD'] # 4 values\n", - "capitals = ['Trenton', 'Albany', 'Harrisburg'] # 3 values\n", - "\n", - "for s, c in zip(states, capitals):\n", - " print('The capital city of %s state is %s' % (s, c))" - ], - "execution_count": 0, - "outputs": [] - }, { "cell_type": "markdown", "metadata": { @@ -1221,9 +1128,8 @@ "source": [ "# SETS\n", "\n", - "Copied from https://www.geeksforgeeks.org/sets-in-python/\n", - "\n", - "A Set is an unordered collection data type that is iterable, MUTABLE and has NO DUPLICATE elements. Python's set class represents the mathematical notion of a set." + "A Set is collection, unordered, iterable, mutable, no duplicate elements. \n", + "Think of sets in math." ] }, { @@ -1493,16 +1399,35 @@ "colab": {} }, "source": [ - "d1 = {}\n", + "# empty dictionary\n", + "d1 = {} # d1 = dict()\n", + "\n", + "# add key-value pairs to the dictionary\n", "d1['a'] = 12\n", "d1['b'] = 3\n", "d1['c'] = 47\n", "d1['d'] = 18\n", + "\n", + "# show the dictionary\n", "d1" ], "execution_count": 0, "outputs": [] }, + { + "cell_type": "code", + "metadata": { + "id": "dr-R17LVKgGW", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# access value by key\n", + "print(d1['c'])" + ], + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "metadata": { @@ -1559,6 +1484,117 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "markdown", + "metadata": { + "id": "FlT-YzvkaVrQ", + "colab_type": "text" + }, + "source": [ + "## Merge two dictionaries\n", + "update() method adds items to the dictionary if the key is not in the dictionary. If the key is in the dictionary, it updates the key with the new value." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "XB7hTMsLadrh", + "colab_type": "code", + "colab": {} + }, + "source": [ + "nfl_team_list_1 = {\n", + " 'Georgia': 'Atlanta Falcons',\n", + " 'New York': 'Buffalo Bills',\n", + " 'New Jersey': 'New York Giants',\n", + " 'Pennsylvania': 'Philadelphia Eagles'\n", + "}\n", + "nfl_team_list_2 = {\n", + " 'Arizona': 'Arizona Cardinals',\n", + " 'New York': 'Buffalo Bills',\n", + " 'New Jersey': 'New York Jets',\n", + " 'Pennsylvania': 'Pittsburgh Steelers',\n", + " 'Illinois': 'Chicago Bears'\n", + "}" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "mXQ7SbA7I-tR", + "colab_type": "code", + "colab": {} + }, + "source": [ + "nfl_team_list_1.update(nfl_team_list_2)\n", + "nfl_team_list_1" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1kPSFdoE9bUz", + "colab_type": "text" + }, + "source": [ + "## Remove items from a dictionary" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "0DP-VkuX9gbb", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# pop() removes an item with speficifed key and returns the value\n", + "val = d1.pop('c')\n", + "print(val)\n", + "print(d1)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "gjiqArH59gfl", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# del\n", + "car = {\n", + " 'brand': 'Ford',\n", + " 'model': 'Explorer',\n", + " 'year': 2017,\n", + " 'price': 20000\n", + "}\n", + "car" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "W6XHJwqPZnRy", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# del keyword delete item by key\n", + "del car['price']\n", + "print(car)" + ], + "execution_count": 0, + "outputs": [] + }, { "cell_type": "markdown", "metadata": { @@ -1577,6 +1613,7 @@ "colab": {} }, "source": [ + "d1 = {'a': 12, 'b': 3, 'c': 47, 'd': 18}\n", "d1" ], "execution_count": 0, @@ -1628,7 +1665,7 @@ "colab_type": "text" }, "source": [ - "# Data Structure Examples" + "# More Examples" ] }, { @@ -1759,7 +1796,13 @@ "colab": {} }, "source": [ - "" + "list1 = [3, 4, 5, 6, 2, 1, 4, 9, 0, -2, 7]\n", + "list2 = [-3, 8, 5, 6, 2, 1, -5, 9, 12, -2, 11]\n", + "\n", + "def common_list(l1, l2):\n", + " pass # your code here\n", + "\n", + "print(common_list(list1, list2))\n" ], "execution_count": 0, "outputs": [] @@ -1771,7 +1814,7 @@ "colab_type": "text" }, "source": [ - "2. Write a function that takes a list of numbers and makes a new list of only the first and last elements of the given list. " + "2. Write a function that takes a list of numbers and returns the minimum and maximum numbers as a tuple. " ] }, { @@ -1782,7 +1825,12 @@ "colab": {} }, "source": [ - "" + "num_list = [3, 4, 5, 6, 2, 1, 4, 9, 0, -2, 7]\n", + "\n", + "def min_max(n_list):\n", + " pass # your code here\n", + "\n", + "print(min_max(num_list))\n" ], "execution_count": 0, "outputs": [] @@ -1811,7 +1859,13 @@ "colab": {} }, "source": [ - "" + "def fibonacci_numbers(up_to=10):\n", + " # first two fibonacci numbers\n", + " fibs = [0, 1]\n", + " \n", + " pass # your code here\n", + "\n", + "print(fibonacci_numbers())" ], "execution_count": 0, "outputs": [] @@ -1834,7 +1888,11 @@ "colab": {} }, "source": [ - "" + "def reverse_string(str):\n", + "\n", + " pass # your code here\n", + "\n", + "print(reverse_string('Friday'))" ], "execution_count": 0, "outputs": [] @@ -1857,11 +1915,37 @@ "colab": {} }, "source": [ - "" + "def reverse_words(str):\n", + "\n", + " pass # your code here\n", + "\n", + "str1 = 'Tom chases Jerry'\n", + "print(reverse_words(str1))" ], "execution_count": 0, "outputs": [] }, + { + "cell_type": "markdown", + "metadata": { + "id": "62OQOTQs0k5K", + "colab_type": "text" + }, + "source": [ + "6. Given a list of stock records, find out the total number of shares for each stock.\n", + "\n", + "Symbol, Price, Shares
\n", + "IBM, 142.3, 50
\n", + "MSFT, 170.21, 100
\n", + "GOOG, 1469.32, 15
\n", + "IBM, 142.3, 20
\n", + "C, 74.78, 100
\n", + "CAT, 133.73, 80
\n", + "C, 74.78, 200
\n", + "GOOG, 1469.32, 7
\n", + "C, 74.78, 80
" + ] + }, { "cell_type": "code", "metadata": { @@ -1874,6 +1958,40 @@ ], "execution_count": 0, "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hi5vnh-p8J1Y", + "colab_type": "text" + }, + "source": [ + "7. Given a list of stock buy/sell records, find out net values for each stock, and the total net value\n", + "\n", + "Symbol, Price, Shares, Buy/Sell
\n", + "IBM, 142.3, 50, Buy
\n", + "MSFT, 170.21, 100, Buy
\n", + "GOOG, 1469.32, 15, Buy
\n", + "IBM, 142.3, 20, Sell
\n", + "C, 74.78, 100, Buy
\n", + "CAT, 133.73, 80, Buy
\n", + "C, 74.78, 200, Buy
\n", + "GOOG, 1469.32, 7, Sell
\n", + "C, 74.78, 80, Sell
" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "IHOG8kbF8ucE", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] } ] } \ No newline at end of file