{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial A5 – Solutions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__1__ Make a list of data objects, each of a different type you know. Print the elements of the list and their type to the screen by index." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "string \n", "1 \n", "1.0 \n", "1j \n", "[] \n", "{} \n" ] } ], "source": [ "list_ = [\"string\", 1, 1.0, 1j, [], {},]\n", "\n", "print(list_[0], type(list_[0]))\n", "print(list_[1], type(list_[1]))\n", "print(list_[2], type(list_[2]))\n", "print(list_[3], type(list_[3]))\n", "print(list_[4], type(list_[4]))\n", "print(list_[5], type(list_[5]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__2__ Create a dictionary that represents a shopping list and store it in a variable: Use three things you need\n", "to buy as *keys* and add how much you want to buy of them as *values*. Than add another item to the existing dictionary. Print the dictionary keys to the screen." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dict_keys(['apples', 'bread', 'onions', 'noodles'])\n" ] } ], "source": [ "buy = {\n", " \"apples\": 6,\n", " \"bread\": 1,\n", " \"onions\": 2,\n", "}\n", "\n", "buy[\"noodles\"] = 1\n", "\n", "print(buy.keys())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Advanced 1__ Take the points `(0, 0)`, `(0, 1)`, `(1, 0)` and `(1, 1)`.\n", "\n", " - Store each point in a variable as a tuple. Make an empty list and append each point to the list. " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[(0, 0), (0, 1), (1, 0), (1, 1)]\n" ] } ], "source": [ "p1 = (0, 0)\n", "p2 = (0, 1)\n", "p3 = (1, 0)\n", "p4 = (1, 1)\n", "\n", "list_ = []\n", "list_.append(p1)\n", "list_.append(p2)\n", "list_.append(p3)\n", "list_.append(p4)\n", "\n", "print(list_)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Change the second point `(0, 1)` to `(0, -1)` by overwriting the variable in which it is stored. What happens to this point in the list?" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(0, -1)\n", "[(0, 0), (0, 1), (1, 0), (1, 1)]\n" ] } ], "source": [ "p2 = (0, -1)\n", "print(p2)\n", "print(list_)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Change the third point `(1, 0)` to `(-1, 0)` by indexing this point stored in the list. What happens to this point outside of the list?" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 0)\n", "[(0, 0), (0, 1), (-1, 0), (1, 1)]\n" ] } ], "source": [ "list_[2] = (-1, 0)\n", "print(p3)\n", "print(list_)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now store each of the initial points (`(0, 0)`, `(0, 1)`, `(1, 0)` and `(1, 1)`) in variables as lists and append them to a list." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0, 0], [0, 1], [1, 0], [1, 1]]\n" ] } ], "source": [ "p1 = [0, 0]\n", "p2 = [0, 1]\n", "p3 = [1, 0]\n", "p4 = [1, 1]\n", "\n", "list_ = []\n", "list_.append(p1)\n", "list_.append(p2)\n", "list_.append(p3)\n", "list_.append(p4)\n", "\n", "print(list_)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Change the second point `[0, 1]` to `[0, -1]` by overwriting the *y*-component of the variable in which it is stored. What happens to this point in the list?" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, -1]\n", "[[0, 0], [0, -1], [1, 0], [1, 1]]\n" ] } ], "source": [ "p2[1] = -1\n", "print(p2)\n", "print(list_)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Change the third point `[1, 0]` to `[-1, 0]` by indexing this point stored in the list and overwriting the its *x*-component. What happens to this point outside of the list?" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-1, 0]\n", "[[0, 0], [0, -1], [-1, 0], [1, 1]]\n" ] } ], "source": [ "list_[2][0] = -1\n", "print(p3)\n", "print(list_)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Advanced 2__ Take the string `\"High Performance Computing\"` and\n", " - convert this into a list." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['H', 'i', 'g', 'h', ' ', 'P', 'e', 'r', 'f', 'o', 'r', 'm', 'a', 'n', 'c', 'e', ' ', 'C', 'o', 'm', 'p', 'u', 't', 'i', 'n', 'g']\n" ] } ], "source": [ "string = \"High Performance Computing\"\n", "list_ = list(string)\n", "print(list_)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Use the tuple `tuple(\"abcdefghijklmnopqrstuvwxyz \")` to map the letters in the string to their index in the alphabet. Tipp: Use the method `tuple.index` and the built-in function `map`. Map takes a function as the first argument and a sequence (e.g. a string as the second. The function is applied to each element of the sequence. Convert the return value of `map()` to a list. Use `string.casefold()` to map also upper case characters. " ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[7, 8, 6, 7, 26, 15, 4, 17, 5, 14, 17, 12, 0, 13, 2, 4, 26, 2, 14, 12, 15, 20, 19, 8, 13, 6]\n" ] } ], "source": [ "mapping_tuple = tuple(\"abcdefghijklmnopqrstuvwxyz \")\n", "map_ = map(mapping_tuple.index, string.casefold())\n", "map_ = list(map_)\n", "print(map_)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Create a dictionary from the map and the list that represents this mapping (`d = {\"letter\": mapped_index}`). Tipp: Use `zip()`." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "run_control": { "marked": true } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'H': 7, 'i': 8, 'g': 6, 'h': 7, ' ': 26, 'P': 15, 'e': 4, 'r': 17, 'f': 5, 'o': 14, 'm': 12, 'a': 0, 'n': 13, 'c': 2, 'C': 2, 'p': 15, 'u': 20, 't': 19}\n" ] } ], "source": [ "dict_ = dict(zip(list_, list(map_)))\n", "print(dict_)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Advanced 3__ Consider the following dictionary of random floating values associated with random string keys:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "dictionary = {\n", " 'afsgfabkpetv': 0.50996581835599997,\n", " 'ajpjggwdnbbv': 0.48394650530200001,\n", " 'bbsqfnzsaerh': 0.94967251027800004,\n", " 'bmkkubxijsgoi': 0.73021130082789998,\n", " 'bmmqvyintpoat': 0.47911570513349999,\n", " 'ccgasifxzngp': 0.32569065293400001,\n", " 'cebmdkidwwyt': 0.97307019405200001,\n", " 'cismykmadevcya': 0.20194133057088001,\n", " 'cjktphzhhbvrn': 0.27046062498669998,\n", " 'ckdgztgvksmid': 0.3741114083232,\n", " 'cwnukrruweshiuh': 0.53529824780703295,\n", " 'cwthembmixxmg': 0.089824943543700006,\n", " 'dasjwthqotarfgz': 0.48673599515272398,\n", " 'dgoxsygqjkkiv': 0.49586035923279997,\n", " 'dijwhwnubajohkf': 0.074657439369638007,\n", " 'dszizjhbozuuy': 0.19034104761039999,\n", " 'xcvhdthqotarfgk': 0.48673599515272398,\n", " 'ehuzgmflwhxcnmv': 0.027337878930186001,\n", " }" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Use the built-in properties of sets to find out whether any two values are the same. How could you check if a set already contains a dictionary value? Could two keys ever be the same?" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Number of values in dict equal to unique values?\n", "len(set(dictionary.values())) == len(dictionary.values())" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Value (dict[key]) in set?\n", "dictionary['xcvhdthqotarfgk'] in set(dictionary.values())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Keys in dictionary have to be unique by definition.*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Compute the sum of all values in the dictionary." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7.684977957563385" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(dictionary.values())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Make a set of unique letters found in all dictionary keys. Tipp: Use the string method `str.join()`." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a',\n", " 'b',\n", " 'c',\n", " 'd',\n", " 'e',\n", " 'f',\n", " 'g',\n", " 'h',\n", " 'i',\n", " 'j',\n", " 'k',\n", " 'l',\n", " 'm',\n", " 'n',\n", " 'o',\n", " 'p',\n", " 'q',\n", " 'r',\n", " 's',\n", " 't',\n", " 'u',\n", " 'v',\n", " 'w',\n", " 'x',\n", " 'y',\n", " 'z'}" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set(\"\".join(dictionary.keys()))" ] } ], "metadata": { "anaconda-cloud": {}, "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.10" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "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": false } }, "nbformat": 4, "nbformat_minor": 2 }