From e8bb889d9bb4fbe5b4fd12df268dcfc682edb145 Mon Sep 17 00:00:00 2001 From: Christopher Arndt Date: Wed, 8 May 2024 16:59:06 +0200 Subject: [PATCH] Add notebook with solutions for uebung for dictionaries developed during the course Signed-off-by: Christopher Arndt --- loesungen/dictionaries_aufgabe.ipynb | 143 +++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 loesungen/dictionaries_aufgabe.ipynb diff --git a/loesungen/dictionaries_aufgabe.ipynb b/loesungen/dictionaries_aufgabe.ipynb new file mode 100644 index 0000000..574f08d --- /dev/null +++ b/loesungen/dictionaries_aufgabe.ipynb @@ -0,0 +1,143 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Rekapitulation Lösung Dictionaries" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "# Ausgangslage\n", + "\n", + "salaries_1 = {'Anna': 2000, 'Mark': 3000, 'Judith': 3500, 'Thomas': 2500}\n", + "salaries_2 = {'Barbara': 3000, 'Elke': 3300, 'Michael': 2800, 'Johann': 2000}" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Anna': 2000, 'Mark': 3000, 'Judith': 3500, 'Thomas': 2500}\n", + "salaries_1: {'Anna': 2000, 'Mark': 3000, 'Judith': 3500, 'Thomas': 2500}\n", + "salaries_all: {'Anna': 2000, 'Mark': 3000, 'Judith': 3500, 'Thomas': 2500, 'Barbara': 3000, 'Elke': 3300, 'Michael': 2800, 'Johann': 2000}\n", + "Summe: 22100\n" + ] + } + ], + "source": [ + "salaries_all = {}\n", + "\n", + "for key in salaries_1:\n", + " salaries_all[key] = salaries_1[key]\n", + "\n", + "print(salaries_all)\n", + "\n", + "for key in salaries_2:\n", + " salaries_all[key] = salaries_2[key]\n", + "\n", + "print(\"salaries_1:\", salaries_1)\n", + "print(\"salaries_all:\", salaries_all)\n", + "\n", + "\n", + "print(\"Summe:\", sum(salaries_all.values()))" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "salaries_1: {'Anna': 2000, 'Mark': 3000, 'Judith': 3500, 'Thomas': 2500}\n", + "salaries_all: {'Anna': 2000, 'Mark': 3000, 'Judith': 3500, 'Thomas': 2500}\n", + "salaries_1: {'Anna': 2000, 'Mark': 3000, 'Judith': 3500, 'Thomas': 2500}\n", + "salaries_2: {'Barbara': 3000, 'Elke': 3300, 'Michael': 2800, 'Johann': 2000}\n", + "salaries_all: {'Anna': 2000, 'Mark': 3000, 'Judith': 3500, 'Thomas': 2500, 'Barbara': 3000, 'Elke': 3300, 'Michael': 2800, 'Johann': 2000}\n", + "8\n" + ] + } + ], + "source": [ + "salaries_all = salaries_1.copy()\n", + "\n", + "print(\"salaries_1:\", salaries_1)\n", + "print(\"salaries_all:\", salaries_all)\n", + "\n", + "salaries_all.update(salaries_2)\n", + "\n", + "print(\"salaries_1:\", salaries_1)\n", + "print(\"salaries_2:\", salaries_2)\n", + "print(\"salaries_all:\", salaries_all)\n", + "\n", + "print(len(salaries_all))" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Anna': 2200.0, 'Mark': 3300.0000000000005, 'Judith': 3850.0000000000005, 'Thomas': 2750.0, 'Barbara': 3300.0000000000005, 'Elke': 3630.0000000000005, 'Michael': 3080.0000000000005, 'Johann': 2200.0}\n" + ] + } + ], + "source": [ + "# Alternative 1\n", + "salaries_new = salaries_all.copy()\n", + "salaries_new.update((key, value * 1.1) for key, value in salaries_new.items())\n", + "\n", + "# Alternative 2\n", + "results = []\n", + "for key, value in salaries_all.items():\n", + " results.append((key, value * 1.1))\n", + "salaries_new = dict(results)\n", + "\n", + "# Alternative 3\n", + "salaries_new = {}\n", + "for key, value in salaries_all.items():\n", + " salaries_new[key] = value * 1.1\n", + "\n", + "print(salaries_new)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "softed", + "language": "python", + "name": "softed" + }, + "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.11.8" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}