From b0222dbe4ef4ebc5fa7e3a8db4f342e8c1c08b85 Mon Sep 17 00:00:00 2001 From: Luigi Selmi <luigiselmi@gmail.com> Date: Tue, 23 Jul 2024 17:40:58 +0200 Subject: [PATCH] dict from lists --- python/scipy-numpy-cheat-sheet.ipynb | 158 ++++++++++++++++++++------- 1 file changed, 119 insertions(+), 39 deletions(-) diff --git a/python/scipy-numpy-cheat-sheet.ipynb b/python/scipy-numpy-cheat-sheet.ipynb index 151d9c9..bda5fd2 100755 --- a/python/scipy-numpy-cheat-sheet.ipynb +++ b/python/scipy-numpy-cheat-sheet.ipynb @@ -36,7 +36,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -66,7 +66,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -83,7 +83,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -92,7 +92,7 @@ "{1, 2, 3, 'Paris', 'Rome', 'a', 'b'}" ] }, - "execution_count": 13, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -111,7 +111,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -120,7 +120,7 @@ "{1, 3}" ] }, - "execution_count": 11, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -140,7 +140,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -167,7 +167,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -192,7 +192,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -217,7 +217,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -242,7 +242,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -267,7 +267,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -276,7 +276,7 @@ "7" ] }, - "execution_count": 8, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -288,7 +288,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -312,7 +312,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -321,7 +321,7 @@ "[5, 8]" ] }, - "execution_count": 10, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -339,7 +339,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ @@ -348,7 +348,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -361,7 +361,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -370,7 +370,7 @@ "1" ] }, - "execution_count": 20, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -388,7 +388,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 17, "metadata": {}, "outputs": [ { @@ -404,7 +404,7 @@ "0.1" ] }, - "execution_count": 11, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -425,7 +425,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ @@ -441,7 +441,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -460,7 +460,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 20, "metadata": {}, "outputs": [ { @@ -469,7 +469,7 @@ "['Pluto', 'Qua']" ] }, - "execution_count": 18, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -487,7 +487,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 21, "metadata": {}, "outputs": [ { @@ -496,7 +496,7 @@ "['Pluto', 'Qua']" ] }, - "execution_count": 19, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } @@ -514,7 +514,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 22, "metadata": {}, "outputs": [ { @@ -526,7 +526,7 @@ " [(4, 1), (4, 2), (4, 3), (4, 4)]]" ] }, - "execution_count": 20, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } @@ -545,7 +545,7 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": 23, "metadata": {}, "outputs": [ { @@ -554,7 +554,7 @@ "['Pluto', 'Qua']" ] }, - "execution_count": 70, + "execution_count": 23, "metadata": {}, "output_type": "execute_result" } @@ -573,7 +573,7 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 24, "metadata": {}, "outputs": [], "source": [ @@ -584,7 +584,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 25, "metadata": {}, "outputs": [ { @@ -602,7 +602,7 @@ "[None, None, None]" ] }, - "execution_count": 42, + "execution_count": 25, "metadata": {}, "output_type": "execute_result" } @@ -620,7 +620,7 @@ }, { "cell_type": "code", - "execution_count": 81, + "execution_count": 26, "metadata": {}, "outputs": [ { @@ -629,7 +629,7 @@ "['Pippo', 'Pluto', 'Topolino']" ] }, - "execution_count": 81, + "execution_count": 26, "metadata": {}, "output_type": "execute_result" } @@ -647,7 +647,7 @@ }, { "cell_type": "code", - "execution_count": 83, + "execution_count": 27, "metadata": {}, "outputs": [ { @@ -656,7 +656,7 @@ "[5, 4, 2]" ] }, - "execution_count": 83, + "execution_count": 27, "metadata": {}, "output_type": "execute_result" } @@ -665,6 +665,86 @@ "list(cartoon_characters.values())" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A dictionary can be created from two or more lists" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [], + "source": [ + "character_names = ['Pippo', 'Pluto', 'Paperino']\n", + "character_score = [5, 8, 4]" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [], + "source": [ + "character_score_dict = {'Name': character_names, 'Score': character_score}" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": {}, + "outputs": [], + "source": [ + "def find_score(name, dict):\n", + " score = -1\n", + " for i in range(len(dict)): \n", + " if (dict['Name'][i] == name):\n", + " score = dict['Score'][i]\n", + " return score" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 86, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "find_score('Pluto', character_score_dict)" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[5, 8, 4]" + ] + }, + "execution_count": 84, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "character_score_dict['Score']" + ] + }, { "cell_type": "markdown", "metadata": {},