{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Fortgeschrittene Dictionaries\n", "\n", "Anders als manche der anderen Daten Strukturen mit denen wir gearbeitet haben, haben wir für Dictionaries die meisten der wirklich nützlichen Methoden während unseres Kurses bereits kennengelernt. Für die Vollständigkeit hier noch einige weitere:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "d = {'k1':1,'k2':2}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionary Abstraktion\n", "\n", "Genau wie beim Listen Abstraktion haben auch Dictionaries ihre eigene Version von Abstraktion (comprehension), um sie schnell erstellen zu können. Es wird nicht so häufig verwendet, dennoch wollen wir ein Beispiel betrachten:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{x:x**2 for x in range(10)}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Einer der Gründe dafür, dass es nicht so häufig verwendet wird ist, dass es schwer ist, Key Namen zu strukturieren, sofern sie nicht auf den Werten basieren.\n", "\n", "## Iteration\n", "\n", "Wir können über Dictionaries iterieren, indem wir die iter() Methode verwenden. Iterationen sind über Keys, Values und Items möglich. Zum Beispiel:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "k1\n", "k2\n" ] } ], "source": [ "for k in iter(d.keys()):\n", " print(k)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n" ] } ], "source": [ "for v in iter(d.values()):\n", " print(v)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('k1', 1)\n", "('k2', 2)\n" ] } ], "source": [ "for item in iter(d.items()):\n", " print(item)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Anzeigen\n", "\n", "Wir können durch items(), keys() und values() anzeigen lassen, was sich im Dictionary befindet." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_items([('k1', 1), ('k2', 2)])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.items()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['k1', 'k2'])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.keys()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_values([1, 2])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.values()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Toll! Jetzt solltet ihr selbstbewusst im Umgang mit der Vielzahl an Methoden sein, die euch für Dicitonaries zur Verfügung stehen." ] } ], "metadata": { "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.1" } }, "nbformat": 4, "nbformat_minor": 2 }