From a277eb97d3c58381f195cfeb08b2950eb8948fa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=A4schke?= <jaeschke@l3s.de> Date: Sun, 17 Jun 2018 18:13:16 +0200 Subject: [PATCH] bis Kap. 11.5 --- img/zustandsdiagramm_dict.svg | 825 ++++++++++++++++++++++++++++++++++ notebooks/seminar11.ipynb | 360 ++++++++++++++- 2 files changed, 1183 insertions(+), 2 deletions(-) create mode 100644 img/zustandsdiagramm_dict.svg diff --git a/img/zustandsdiagramm_dict.svg b/img/zustandsdiagramm_dict.svg new file mode 100644 index 0000000..d4f9fb4 --- /dev/null +++ b/img/zustandsdiagramm_dict.svg @@ -0,0 +1,825 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<!-- Created with Inkscape (http://www.inkscape.org/) --> + +<svg + xmlns:dc="http://purl.org/dc/elements/1.1/" + xmlns:cc="http://creativecommons.org/ns#" + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" + xmlns:svg="http://www.w3.org/2000/svg" + xmlns="http://www.w3.org/2000/svg" + xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" + xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" + width="89.779518mm" + height="40.247566mm" + viewBox="0 0 89.779521 40.247566" + version="1.1" + id="svg8" + inkscape:version="0.92.1 r15371" + sodipodi:docname="zustandsdiagramm_dict.svg"> + <defs + id="defs2"> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM" + style="overflow:visible" + inkscape:isstock="true"> + <path + id="path4657" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" + inkscape:connector-curvature="0" /> + </marker> + <marker + inkscape:stockid="Arrow1Send" + orient="auto" + refY="0" + refX="0" + id="Arrow1Send" + style="overflow:visible" + inkscape:isstock="true"> + <path + id="path4527" + d="M 0,0 5,-5 -12.5,0 5,5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="matrix(-0.2,0,0,-0.2,-1.2,0)" + inkscape:connector-curvature="0" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-6" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-6" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-4" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-8" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1-1" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9-0" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1-9" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9-01" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1-1-4" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9-0-8" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1-1-8" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9-0-2" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1-9-9" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9-01-8" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1-1-0" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9-0-9" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1-6" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9-7" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1-1-0-9" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9-0-9-8" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1-6-2" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9-7-6" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1-9-7" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9-01-5" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1-1-9" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9-0-3" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1-99" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9-79" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1-14" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9-3" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1-14-4" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9-3-2" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1-14-4-8" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9-3-2-5" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1-99-1" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9-79-3" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1-1-9-0" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9-0-3-6" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1-9-7-4" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9-01-5-4" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1-6-2-7" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9-7-6-5" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1-1-0-9-3" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9-0-9-8-8" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1-14-4-8-6" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9-3-2-5-5" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + </defs> + <sodipodi:namedview + id="base" + pagecolor="#ffffff" + bordercolor="#666666" + borderopacity="1.0" + inkscape:pageopacity="0.0" + inkscape:pageshadow="2" + inkscape:zoom="2.0065268" + inkscape:cx="226.70579" + inkscape:cy="48.579679" + inkscape:document-units="mm" + inkscape:current-layer="layer1" + showgrid="false" + inkscape:window-width="1920" + inkscape:window-height="1009" + inkscape:window-x="0" + inkscape:window-y="34" + inkscape:window-maximized="1" + fit-margin-top="0" + fit-margin-left="0" + fit-margin-right="0" + fit-margin-bottom="0" /> + <metadata + id="metadata5"> + <rdf:RDF> + <cc:Work + rdf:about=""> + <dc:format>image/svg+xml</dc:format> + <dc:type + rdf:resource="http://purl.org/dc/dcmitype/StillImage" /> + <dc:title></dc:title> + </cc:Work> + </rdf:RDF> + </metadata> + <g + inkscape:label="Ebene 1" + inkscape:groupmode="layer" + id="layer1" + transform="translate(-31.867578,-20.187424)"> + <rect + y="23.569387" + x="92.952316" + height="36.733105" + width="9.4141617" + id="rect4501-1-1" + style="opacity:0.99823531;fill:#aaccff;fill-opacity:0.60784314;stroke:#0067ff;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" /> + <rect + y="23.569387" + x="104.97993" + height="23.415102" + width="16.534668" + id="rect4501-1" + style="opacity:0.99823531;fill:#aaccff;fill-opacity:0.60784314;stroke:#0067ff;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" /> + <rect + y="23.569387" + x="46.469715" + height="28.557695" + width="16.534666" + id="rect4501" + style="opacity:0.99823531;fill:#aaccff;fill-opacity:0.60784314;stroke:#0067ff;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" /> + <text + xml:space="preserve" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + x="36.762238" + y="27.505676" + id="text4501-1"><tspan + sodipodi:role="line" + x="38.03141" + y="27.505676" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;font-family:FreeMono;-inkscape-font-specification:'FreeMono, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-feature-settings:normal;text-align:center;writing-mode:lr-tb;text-anchor:middle;stroke-width:0.26458332px" + id="tspan4499-9">hist </tspan></text> + <text + id="text4527-3" + y="28.085644" + x="50.53973" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + xml:space="preserve"><tspan + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px" + y="28.085644" + x="50.53973" + sodipodi:role="line" + id="tspan4735">'P'</tspan><tspan + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px" + y="32.421616" + x="50.53973" + sodipodi:role="line" + id="tspan4739">'a'</tspan><tspan + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px" + y="36.757587" + x="50.53973" + sodipodi:role="line" + id="tspan4741">'e'</tspan><tspan + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px" + y="41.093563" + x="50.53973" + sodipodi:role="line" + id="tspan4743">'g'</tspan><tspan + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px" + y="45.429535" + x="50.53973" + sodipodi:role="line" + id="tspan4745">'i'</tspan><tspan + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px" + y="49.765507" + x="50.53973" + sodipodi:role="line" + id="tspan4749">'p'</tspan></text> + <text + xml:space="preserve" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:1.25;font-family:'DejaVu Sans Mono';-inkscape-font-specification:'DejaVu Sans Mono';letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332" + x="47.055614" + y="22.759174" + id="text4653-5"><tspan + sodipodi:role="line" + id="tspan4651-3" + x="47.055614" + y="22.759174" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:3.52777767px;font-family:FreeSans;-inkscape-font-specification:'FreeSans, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-feature-settings:normal;text-align:start;writing-mode:lr-tb;text-anchor:start;stroke-width:0.26458332">dict</tspan></text> + <path + style="fill:none;stroke:#000000;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#TriangleOutM-1)" + d="m 54.112092,26.515048 h 3.164673" + id="path5544" + inkscape:connector-curvature="0" /> + <path + style="fill:none;stroke:#000000;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#TriangleOutM-1-1)" + d="m 54.112092,30.933415 h 3.164673" + id="path5544-8" + inkscape:connector-curvature="0" /> + <path + style="fill:none;stroke:#000000;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#TriangleOutM-1-9)" + d="m 54.112092,35.351783 h 3.164673" + id="path5544-3" + inkscape:connector-curvature="0" /> + <flowRoot + xml:space="preserve" + id="flowRoot4584" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:80px;line-height:100%;font-family:Futura;-inkscape-font-specification:Futura;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + transform="matrix(0.26458333,0,0,0.26458333,21.859876,20.187424)"><flowRegion + id="flowRegion4586"><rect + id="rect4588" + width="246.68236" + height="136.73251" + x="-17.620169" + y="77.571198" /></flowRegion><flowPara + id="flowPara4590"></flowPara></flowRoot> <text + id="text4527-3-2" + y="28.000977" + x="60.311161" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + xml:space="preserve"><tspan + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px" + y="28.000977" + x="60.311161" + sodipodi:role="line" + id="tspan4749-0">1</tspan><tspan + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px" + y="32.336948" + x="60.311161" + sodipodi:role="line" + id="tspan4806">2</tspan><tspan + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px" + y="36.67292" + x="60.311161" + sodipodi:role="line" + id="tspan4808">1</tspan><tspan + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px" + y="41.008896" + x="60.311161" + sodipodi:role="line" + id="tspan4810">1</tspan><tspan + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px" + y="45.344868" + x="60.311161" + sodipodi:role="line" + id="tspan4812">1</tspan><tspan + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px" + y="49.68084" + x="60.311161" + sodipodi:role="line" + id="tspan4814">1</tspan></text> + <path + style="fill:none;stroke:#000000;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#TriangleOutM-1-6)" + d="m 54.112092,39.770148 h 3.164673" + id="path5544-2" + inkscape:connector-curvature="0" /> + <path + style="fill:none;stroke:#000000;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#TriangleOutM-1-1-0)" + d="m 54.112092,44.188513 h 3.164673" + id="path5544-8-1" + inkscape:connector-curvature="0" /> + <path + style="fill:none;stroke:#000000;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#TriangleOutM-1-9-9)" + d="m 54.112092,48.606878 h 3.164673" + id="path5544-3-4" + inkscape:connector-curvature="0" /> + <text + xml:space="preserve" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + x="78.303146" + y="27.505676" + id="text4501-1-4"><tspan + sodipodi:role="line" + x="78.303146" + y="27.505676" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;font-family:FreeMono;-inkscape-font-specification:'FreeMono, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-feature-settings:normal;text-align:center;writing-mode:lr-tb;text-anchor:middle;stroke-width:0.26458332px" + id="tspan4499-9-8">inverse</tspan></text> + <text + id="text4527-3-9" + y="28.000977" + x="96.120186" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + xml:space="preserve"><tspan + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:start;text-anchor:start;stroke-width:0.26458332px" + y="28.000977" + x="96.120186" + sodipodi:role="line" + id="tspan4749-6">1</tspan></text> + <g + id="g6009" + transform="translate(7.4083335,2.0131798)"> + <text + xml:space="preserve" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + x="100.83789" + y="26.011433" + id="text4527-3-8"><tspan + id="tspan4749-8" + sodipodi:role="line" + x="100.83789" + y="26.011433" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px">0</tspan><tspan + id="tspan5552" + sodipodi:role="line" + x="100.83789" + y="30.347404" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px">1</tspan><tspan + id="tspan5554" + sodipodi:role="line" + x="100.83789" + y="34.683376" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px">2</tspan><tspan + id="tspan5556" + sodipodi:role="line" + x="100.83789" + y="39.019352" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px">3</tspan><tspan + id="tspan5558" + sodipodi:role="line" + x="100.83789" + y="43.355324" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px">4</tspan></text> + <text + xml:space="preserve" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + x="110.83282" + y="25.987797" + id="text4527-3-8-9"><tspan + id="tspan5558-1" + sodipodi:role="line" + x="110.83282" + y="25.987797" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px">'g'</tspan><tspan + id="tspan5604" + sodipodi:role="line" + x="110.83282" + y="30.323769" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px">'p'</tspan><tspan + id="tspan5606" + sodipodi:role="line" + x="110.83282" + y="34.65974" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px">'P'</tspan><tspan + id="tspan5610" + sodipodi:role="line" + x="110.83282" + y="38.995716" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px">'e'</tspan><tspan + id="tspan5614" + sodipodi:role="line" + x="110.83282" + y="43.331688" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px">'i'</tspan></text> + <path + inkscape:connector-curvature="0" + id="path5544-7" + d="m 103.72394,24.695253 h 3.16467" + style="fill:none;stroke:#000000;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#TriangleOutM-1-99)" /> + <path + inkscape:connector-curvature="0" + id="path5544-8-3" + d="m 103.72394,29.11362 h 3.16467" + style="fill:none;stroke:#000000;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#TriangleOutM-1-1-9)" /> + <path + inkscape:connector-curvature="0" + id="path5544-3-5" + d="m 103.72394,33.531988 h 3.16467" + style="fill:none;stroke:#000000;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#TriangleOutM-1-9-7)" /> + <path + inkscape:connector-curvature="0" + id="path5544-2-0" + d="m 103.72394,37.950353 h 3.16467" + style="fill:none;stroke:#000000;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#TriangleOutM-1-6-2)" /> + <path + inkscape:connector-curvature="0" + id="path5544-8-1-4" + d="m 103.72394,42.368718 h 3.16467" + style="fill:none;stroke:#000000;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#TriangleOutM-1-1-0-9)" /> + </g> + <text + xml:space="preserve" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:1.25;font-family:'DejaVu Sans Mono';-inkscape-font-specification:'DejaVu Sans Mono';letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332" + x="105.28265" + y="22.759174" + id="text4653-5-9"><tspan + sodipodi:role="line" + id="tspan4651-3-4" + x="105.28265" + y="22.759174" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:3.52777767px;font-family:FreeSans;-inkscape-font-specification:'FreeSans, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-feature-settings:normal;text-align:start;writing-mode:lr-tb;text-anchor:start;stroke-width:0.26458332">list</tspan></text> + <path + style="fill:none;stroke:#000000;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#TriangleOutM-1-14)" + d="m 42.182251,26.450841 h 3.164673" + id="path5544-6" + inkscape:connector-curvature="0" /> + <path + style="fill:none;stroke:#000000;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#TriangleOutM-1-14-4)" + d="m 88.073445,26.450841 h 3.164673" + id="path5544-6-2" + inkscape:connector-curvature="0" /> + <text + xml:space="preserve" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:1.25;font-family:'DejaVu Sans Mono';-inkscape-font-specification:'DejaVu Sans Mono';letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332" + x="92.920387" + y="22.759174" + id="text4653-5-7"><tspan + sodipodi:role="line" + id="tspan4651-3-6" + x="92.920387" + y="22.759174" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:3.52777767px;font-family:FreeSans;-inkscape-font-specification:'FreeSans, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-feature-settings:normal;text-align:start;writing-mode:lr-tb;text-anchor:start;stroke-width:0.26458332">dict</tspan></text> + <path + style="fill:none;stroke:#000000;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#TriangleOutM-1-14-4-8)" + d="m 99.279941,26.450841 h 4.747009" + id="path5544-6-2-1" + inkscape:connector-curvature="0" + sodipodi:nodetypes="cc" /> + <rect + y="54.080368" + x="104.97993" + height="6.141263" + width="16.534668" + id="rect4501-1-2" + style="opacity:0.99823531;fill:#aaccff;fill-opacity:0.60784314;stroke:#0067ff;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" /> + <g + id="g6009-7" + transform="translate(7.2690043,32.52416)"> + <text + xml:space="preserve" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + x="100.83789" + y="26.011433" + id="text4527-3-8-3"><tspan + id="tspan5558-8" + sodipodi:role="line" + x="100.83789" + y="26.011433" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px">0</tspan></text> + <text + xml:space="preserve" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + x="110.83282" + y="25.987797" + id="text4527-3-8-9-0"><tspan + id="tspan5614-5" + sodipodi:role="line" + x="110.83282" + y="25.987797" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:center;text-anchor:middle;stroke-width:0.26458332px">'a'</tspan></text> + <path + inkscape:connector-curvature="0" + id="path5544-7-8" + d="m 103.72394,24.695253 h 3.16467" + style="fill:none;stroke:#000000;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#TriangleOutM-1-99-1)" /> + </g> + <text + xml:space="preserve" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:1.25;font-family:'DejaVu Sans Mono';-inkscape-font-specification:'DejaVu Sans Mono';letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332" + x="105.28265" + y="53.270157" + id="text4653-5-9-9"><tspan + sodipodi:role="line" + id="tspan4651-3-4-5" + x="105.28265" + y="53.270157" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:3.52777767px;font-family:FreeSans;-inkscape-font-specification:'FreeSans, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-feature-settings:normal;text-align:start;writing-mode:lr-tb;text-anchor:start;stroke-width:0.26458332">list</tspan></text> + <text + id="text4527-3-9-1" + y="58.807114" + x="96.120186" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + xml:space="preserve"><tspan + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-family:FreeSans;-inkscape-font-specification:FreeSans;text-align:start;text-anchor:start;stroke-width:0.26458332px" + y="58.807114" + x="96.120186" + sodipodi:role="line" + id="tspan4749-6-9">2</tspan></text> + <path + style="fill:none;stroke:#000000;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#TriangleOutM-1-14-4-8-6)" + d="m 99.27994,57.256978 h 4.74701" + id="path5544-6-2-1-4" + inkscape:connector-curvature="0" + sodipodi:nodetypes="cc" /> + </g> +</svg> diff --git a/notebooks/seminar11.ipynb b/notebooks/seminar11.ipynb index 1d4ab1b..f4aabc0 100644 --- a/notebooks/seminar11.ipynb +++ b/notebooks/seminar11.ipynb @@ -220,8 +220,364 @@ "source": [ "Der Operator `in` nutzt unterschiedliche Algorithmen für Listen und assoziative Datenfelder. In Listen durchsucht er die Elemente der Liste der Reihenfolge nach, wie in [Abschnitt 8.6](seminar08.ipynb#8.6-Suche) beschrieben. Wenn die Liste größer wird, dauert die Suche entsprechend länger. \n", "\n", - "Für assoziative Datenfelder nutzt Python eine Datenstruktur, die **Hash-Tabelle** genannt wird. Diese hat eine bemerkenswerte Eigenschaft: der Operator `in` benötigt ungefähr die gleiche Zeit, egal wie viele Einträge das assoziative Datenfeld enthält. In [Abschnitt B.4](seminarb.ipynb#B.4-Hash-Tabellen) wird erklärt, wie das möglich ist, aber die Erklärung ergibt für Sie vermutlich erst Sinn, nachdem Sie ein paar mehr Kapitel durchgearbeitet haben.\n", - "\n" + "Für assoziative Datenfelder nutzt Python eine Datenstruktur, die **Hash-Tabelle** genannt wird. Diese hat eine bemerkenswerte Eigenschaft: der Operator `in` benötigt ungefähr die gleiche Zeit, egal wie viele Einträge das assoziative Datenfeld enthält. In [Abschnitt B.4](seminarb.ipynb#B.4-Hash-Tabellen) wird erklärt, wie das möglich ist, aber die Erklärung ergibt für Sie vermutlich erst Sinn, nachdem Sie ein paar mehr Kapitel durchgearbeitet haben." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 11.2 Assoziative Datenfelder als Sammlung von Zählern\n", + "\n", + "Angenommen wir wollen zählen, wie oft jeder Buchstabe in einer Zeichenkette enthalten ist. Es gibt mehrere Möglichkeiten, wie wir das erledigen könnten:\n", + "\n", + "1. Wir könnten für jeden Buchstaben des Alphabets eine Variable erzeugen - also 30 Variablen. Dann könnten wir die Zeichenkette durchlaufen und für jeden Buchstaben den Wert der entsprechenden Variable um eins erhöhen. Dafür würden wir vermutlich (viele!) verkettete Bedingungen benötigen.\n", + "2. Wir könnten auch eine Liste mit 30 Einträgen erzeugen und jeden Buchstaben in eine Zahl umwandeln (beispielsweise mit der eingebauten Funktion `ord`). Die Zahl entspräche dann dem Index eines Eintrages in der Liste und wir könnten den entsprechenden Wert erhöhen.\n", + "3. Wir könnten ein assoziatives Datenfeld erzeugen mit den Buchstaben als Schlüsseln und den Zählern als zugehörigen Werten. Wenn wir einen Buchstaben zum ersten Mal sehen, dann würden wir einen Eintrag zum assoziativen Datenfeld hinzufügen. Ansonsten würden wir den Wert des bestehenden Eintrages erhöhen.\n", + "\n", + "Jede dieser Varianten erledigt die gleiche Berechnung, aber jede implementiert sie auf eine unterschiedliche Weise.\n", + "\n", + "Eine **Implementierung** (*implementation*) ist eine Weise, eine Berechnung durchzuführen; manche Implementierungen sind besser als andere. Beispielsweise ist ein Vorteil einer Implementierung durch ein assoziatives Datenfeld, dass wir vorab wissen müssen, welche Buchstaben in der Zeichenkette enthalten sind und wir nur Platz für diejenigen Zeichen machen müssen, die tatsächlich enthalten sind.\n", + "\n", + "So könnte der Code für diese Implementierung aussehen:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def histogramm(s):\n", + " d = dict()\n", + " for c in s:\n", + " if c not in d:\n", + " d[c] = 1\n", + " else:\n", + " d[c] += 1\n", + " return d" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Der Name der Funktion ist `histogramm`, das ist ein Begriff aus der Statistik, der eine Sammlung von Zählern (oder Häufigkeiten) bezeichnet.\n", + "\n", + "Die erste Zeile der Funktion erzeugt ein leerzes assoziatives Datenfeld. Die `for`-Schleife durchläuft die Zeichenkette `s`. Bei jedem Schleifendurchlauf wird geprüft, ob das aktuelle Zeichen `c` im assoziativen Datenfeld `d` enthalten ist. Falls nicht, erzeugen wir einen neuen Eintrag mit dem Schlüssel `c` und dem Anfangswert 1 (da wir den Buchstaben bisher einmal gesehen haben). Falls `c` bereits enthalten ist, erhöhen wir den Wert `d[c]` um 1.\n", + "\n", + "So funktioniert das ganze dann:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "h = histogramm(\"Brontosaurier\")\n", + "h" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Im Histogramm sehen wir, dass die Buchstaben `'B'`, `'a'`, usw. einmal enthalten sind; der Buchstabe `'o'` zweimal, `'r'` dreimal.\n", + "\n", + "Assoziative Datenfelder haben eine Methode `get`, die einen Schlüssel sowie einen Vorgabewert erwartet. Falls der Schlüssel im assoziativen Datenfeld enthalten ist, liefert `get` den zugehörigen Wert zurück, ansonsten gibt es den Vorgabewert zurück. Beispielsweise:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "h = histogramm('a')\n", + "h" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "h.get('a', 0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "h.get('b', 0)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Nutzen Sie die Methode `get`, um die Funktion `histogramm` etwas prägnanter zu (re-)implementieren. Sie sollten es dabei schaffen, die `if`-Verzweigung zu entfernen:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Implementieren Sie hier Ihre Funktion histogramm\n", + "def histogramm(s):\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 11.3 Schleifen und assoziative Datenfelder\n", + "\n", + "Wenn Sie ein assoziatives Datenfeld in einer `for`-Schleife nutzen, durchlaufen Sie alle Schlüssel des assoziativen Datenfeldes. Beispielsweise gibt `print_hist` alle Schlüsselund die zugehörigen Werte aus:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def print_hist(h):\n", + " for c in h:\n", + " print(c, h[c])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So sieht die Ausgabe dann au:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "h = histogramm(\"Papagei\")\n", + "print_hist(h)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Wieder sind die Schlüssel ungeordnet. Um die Schlüssel geordnet zu durchlaufen, können wir die eingebaute Funktion `sorted` verwenden:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for key in sorted(h):\n", + " print(key, h[key])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 11.4 Rückwärtsauflösung\n", + "\n", + "Wenn ein assoziatives Datenfeld `d` und ein Schlüssel `k` gegeben sind, ist es einfach, den zugehörigen Wert `v = d[k]` zu ermitteln. Diese Operation wird mit **Auflösung** (*lookup*) bezeichnet.\n", + "\n", + "Was aber tun, wenn `v` gegeben ist und wir `k` finden müssen? Dann haben wir zwei Probleme: erstens könnte es mehr als einen Schlüssel geben, der auf `v` abgebildet wird. Abhängig von der Anwendung ist es uns vielleicht möglich, einen der Schlüssel auszuwählen oder wir müssen eine Liste aller möglichen Schlüssel erstellen. Zweitens gibt es keine einfache Syntax für die **Rückwärtsauflösung** (*reverse lookup*); wir müssen suchen.\n", + "\n", + "Dies ist eine Funktion, die ein assoziatives Datenfeld und einen Wert erwartet und den ersten Schlüssel zurückgibt, der auf den Wert abgebildet wird:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def reverse_lookup(d, v):\n", + " for k in d:\n", + " if d[k] == v:\n", + " return k\n", + " raise LookupError()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Diese Funktion ist ein weiteres Beispiel eines Such-Musters, aber sie nutzt eine Fähigkeit, die wir bisher noch nicht gesehen haben: `raise`. Die **raise-Anweisung** erzeugt eine Ausnahme (*exception*), in diesem Fall erzeugt sie einen `LookupError`, was eine eingebaute Ausnahme ist, die anzeigt, dass eine Auflösungs-Operation fehlgeschlagen ist.\n", + "\n", + "Wenn wir das Ende der Schleife erreichen heißt das, dass `v` nicht als Wert im assoziativen Datenfeld enthalten ist, daher erzeugen wir die Ausnahme.\n", + "\n", + "Hier ist ein Beispiel für eine erfolgreiche Rückwärtsauflösung:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "h = histogramm(\"Papagei\")\n", + "key = reverse_lookup(h, 2)\n", + "key" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Und ein Beispiel für eine nicht erfolgreiche:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "key = reverse_lookup(h, 3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Die Wirkung, wenn wir eine Ausnahme erzeugen, ist die gleiche, wie wenn Python eine erzeugt: es wird ein Stacktrace und eine Fehlermeldung ausgegeben.\n", + "\n", + "Der `raise`-Anweisung kann eine detaillierte Fehlermeldung als optionales Argument übergeben werden. Beispielsweise:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "raise LookupError(\"Der Wert ist nicht im assoziativen Datenfeld enthalten.\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Eine Rückwärtsauflösung ist deutlich langsamer als eine (Vorwärts)Auflösung; wenn wir sie häufig durchführen müssen oder wenn das assoziative Datenfeld groß wird, wird die Performanz unseres Programms leiden." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 11.5 Assoziative Datenfelder und Listen\n", + "\n", + "Listen können als Werte in einem assoziativen Datenfeld verwendet werden! Wenn wir beispielsweise ein assoziatives Datenfeld erhalten, welches Buchstaben auf Häufigkeiten abbildet, möchten wir es vielleicht invertieren, das heißt, ein assoziatives Datenfeld erzeugen, welches Häufigkeiten auf Buchstaben abbildet. Da es mehrere Buchstaben mit der\n", + "gleichen Häufigkeit geben kann, sollte jeder Wert im des assoziativen Datenfeldes eine Liste von Buchstaben sein.\n", + "\n", + "Hier ist eine Funktion, die ein assoziatives Datenfeld invertiert:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def invert_dict(d):\n", + " inverse = dict()\n", + " for key in d:\n", + " val = d[key]\n", + " if val not in inverse:\n", + " inverse[val] = [key]\n", + " else:\n", + " inverse[val].append(key)\n", + " return inverse" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Bei jedem Schleifendurchlauf wird `key` ein Schlüssel von `d` zugewiesen und `val` der entsprechende Wert. Falls `val` nicht in `inverse` enthalten ist (wir es also noch nicht gesehen haben), wird ein neuer Eintrag erzeugt und mit einem **Singleton** (einer Liste mit genau einem Element) initialisiert. Ansonsten haben wir den Wert bereits gesehen, so dass wir den zugehörigen Schlüssel mittels `append` einfach zur Liste hinzufügen können. \n", + "\n", + "Hier ist ein Beispiel:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "hist = histogramm(\"Papagei\")\n", + "hist" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "inverse = invert_dict(hist)\n", + "inverse " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "\n", + "Die Abbildung ist ein Zustandsdiagramm welches `hist` und `inverse` zeigt. Ein assoziatives Datenfeld wird durch eine Kiste mit dem Typ `dict` darüber und den Schlüssel-Wert-Paaren innen repräsentiert. Wenn die Werte ganze Zahlen, Gleitkommazahlen oder Zeichenketten sind, dann zeichnen wir sie innerhalb der Kiste. Listen als Werte zeichnen wir üblicherweise außerhalb der Liste, um das Diagramm einfach zu halten.\n", + "\n", + "Wie das Beispiel zeigt, können Listen Werte in einem assoziativen Datenfeld sein, aber sie können keine Schlüssel sein. Schauen wir, was passiert, wenn wir es trotzdem versuchen:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "t = [1, 2, 3]\n", + "d = dict()\n", + "d[t] = 'oops'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Wie bereits erwähnt, werden assoziative Datenfelder mittels Hash-Tabellen implementiert und das bedeutet, dass die Schlüssel **hashbar** (*hashable*) sein müssen.\n", + "\n", + "Ein **Hash** ist eine Funktion, die einen Wert (jedweder Art) erwartet und eine ganze Zahl zurückliefert. Assoziative Datenfelder verwenden diese ganzen Zahlen, genannt Hashwerte, um Schlüssel-Wert-Paare zu speichern und aufzulösen. \n", + "\n", + "Dieses System funktioniert gut, wenn die Schlüssel unveränderbar (*immutable*) sind. Wenn die Schlüssel jedoch veränderbar sind, wie bei Listen, dann können schlimme Dinge passieren. Wenn wir ein Schlüssel-Wert-Paar erzeuen, dann hasht Python den Schlüssel und speichert den Wert an der entsprechenden Stelle. Wenn wir den Schlüssel verändern und ihn dann noch einmal hashen, dann erhalten wir einen anderen Wert und würden dementsprechend an einer anderen Stelle landen. In diesem Fall könnten wir zwei Einträge für den gleichen Schlüssel haben oder wir finden den Schlüssel nicht. In beiden Fällen würde das assoziative Datenfeld nicht richtig funktionieren. \n", + "\n", + "Aus diesem Grund müssen Schlüssel hashbar sein und veränderbare Typen wie Listen sind es nicht. Die einfachste Möglichkeit, diese Einschränkung zu umgehen liegt darin, Tupel zu verwenden, die wir im nächsten Kapitel kennenlernen werden. \n", + "\n", + "Da assoziative Datenfelder veränderbar sind, können sie nicht als Schlüssel verwendet werden, aber sie *können* als Werte verwendet werden." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 11.6 Memos\n", + "\n", + " " ] }, { -- GitLab