{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "bf18c881", "metadata": { "id": "bf18c881" }, "source": [ "# Diccionarios [2]" ] }, { "cell_type": "code", "execution_count": 1, "id": "4d0416b0", "metadata": { "id": "4d0416b0", "outputId": "464ce298-22a2-47d3-fd97-a5f7dd8bdae3", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'Apolo': 11, 'astronauta': 'Neil Armstrong', 'año': 1969}" ] }, "metadata": {}, "execution_count": 1 } ], "source": [ "historia = {\n", " \"Apolo\": 11,\n", " \"astronauta\": \"Neil Armstrong\",\n", " \"año\": 1969\n", "}\n", "historia" ] }, { "cell_type": "code", "execution_count": 2, "id": "667b1403", "metadata": { "id": "667b1403", "outputId": "a2995e2c-25c7-450a-a7ae-c3c1bd6b00e8", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'Apolo': 'XI', 'astronauta': 'Neil Armstrong', 'año': 1969}" ] }, "metadata": {}, "execution_count": 2 } ], "source": [ "historia[\"Apolo\"] = 'XI' # Cambiamos el valor de la clave Apolo\n", "historia" ] }, { "cell_type": "code", "execution_count": 3, "id": "80fc73ca", "metadata": { "id": "80fc73ca", "outputId": "6a371d9c-0d68-40bd-e509-7d11eb6f39c3", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "True\n" ] } ], "source": [ "print(\"año\" in historia) # True" ] }, { "cell_type": "code", "execution_count": 4, "id": "ea8d6781", "metadata": { "id": "ea8d6781", "outputId": "a39a9735-6718-4efa-ac60-abcdf51cbc0c", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "3\n" ] } ], "source": [ "print(len(historia))" ] }, { "cell_type": "code", "execution_count": 5, "id": "98fd9a06", "metadata": { "id": "98fd9a06", "outputId": "5a1d67dd-c3b4-411d-93d0-b8db9aca1ca9", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'Apolo': 'XI', 'astronauta': 'Neil Armstrong', 'año': 1969, 'módulo': 'Eagle'}" ] }, "metadata": {}, "execution_count": 5 } ], "source": [ "historia[\"módulo\"] = \"Eagle\"\n", "historia" ] }, { "cell_type": "code", "execution_count": 6, "id": "b6841671", "metadata": { "id": "b6841671", "outputId": "f677fd85-3eca-4ceb-c8f8-9bf1f4d760c4", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'Apolo': 'XI', 'año': 1969, 'módulo': 'Eagle'}" ] }, "metadata": {}, "execution_count": 6 } ], "source": [ "historia.pop(\"astronauta\")\n", "historia" ] }, { "cell_type": "markdown", "id": "6b476e12", "metadata": { "id": "6b476e12" }, "source": [ "## Añadir y borrar un elemento" ] }, { "cell_type": "code", "execution_count": 7, "id": "167316ac", "metadata": { "id": "167316ac", "outputId": "aff6a065-d5e8-4d72-a528-4d46d2232e29", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'verde': ['green', 'vert'],\n", " 'rojo': ['red', 'rouge'],\n", " 'azul': ['blue', 'bleu']}" ] }, "metadata": {}, "execution_count": 7 } ], "source": [ "color = {'verde': ['green','vert'],\n", " 'rojo': ['red','rouge'],\n", " 'azul': ['blue','bleu']}\n", "color # diccionario de colores con traducción en inglés y en francés" ] }, { "cell_type": "code", "execution_count": 8, "id": "084f0077", "metadata": { "id": "084f0077", "outputId": "a2bc228d-685f-4fd7-d45f-8d26d1dd1ae3", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'verde': ['green', 'vert'],\n", " 'rojo': ['red', 'rouge'],\n", " 'azul': ['blue', 'bleu'],\n", " 'violeta': ['purple', 'violette']}" ] }, "metadata": {}, "execution_count": 8 } ], "source": [ "color['violeta'] = ['purple','violette']\n", "color" ] }, { "cell_type": "code", "execution_count": 9, "id": "b53dc70d", "metadata": { "id": "b53dc70d", "outputId": "c20635f0-6d8e-4392-d83b-51b8e78248c2", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'verde': ['green', 'vert'],\n", " 'azul': ['blue', 'bleu'],\n", " 'violeta': ['purple', 'violette']}" ] }, "metadata": {}, "execution_count": 9 } ], "source": [ "del(color['rojo'])\n", "color" ] }, { "cell_type": "markdown", "id": "2870f9df", "metadata": { "id": "2870f9df" }, "source": [ "## Operar con valores" ] }, { "cell_type": "markdown", "id": "1907ee62", "metadata": { "id": "1907ee62" }, "source": [ "### Modificando el valor correspondiente a una clave." ] }, { "cell_type": "code", "execution_count": 10, "id": "2f2a7206", "metadata": { "id": "2f2a7206", "outputId": "2fc3a921-058e-476a-d1b7-526d7b89373e", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'Ana': 24, 'Luis': 27, 'Eva': 25, 'Raul': 26}" ] }, "metadata": {}, "execution_count": 10 } ], "source": [ "edad = {'Ana':23, 'Luis':27, 'Eva':25, 'Raul':26}\n", "edad['Ana'] += 1 # es el cumpleaños de Ana\n", "edad" ] }, { "cell_type": "markdown", "id": "b51c7526", "metadata": { "id": "b51c7526" }, "source": [ "### Comparando valores de varias claves" ] }, { "cell_type": "code", "execution_count": 11, "id": "27756e03", "metadata": { "id": "27756e03", "outputId": "04de6d9f-4142-42b7-fbca-078c1dc2c989", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 11 } ], "source": [ "edad['Ana'] < edad['Eva'] < edad['Raul'] # comparando los valores de varias claves" ] }, { "cell_type": "markdown", "id": "cb0c38d1", "metadata": { "id": "cb0c38d1" }, "source": [ "## Listar clave, valor\n", "Usando bucles podemos listar el contenido de la clave y/o el valor de un diccionario." ] }, { "cell_type": "code", "execution_count": 12, "id": "71e062fa", "metadata": { "id": "71e062fa", "outputId": "f92cd15b-1ae3-47d9-fed7-61b014a9189c", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Ana\n", "Luis\n", "Eva\n", "Raul\n" ] } ], "source": [ "for k in edad: # obtenemos solo un listado de la clave\n", " print(k)" ] }, { "cell_type": "code", "execution_count": 13, "id": "5774173e", "metadata": { "id": "5774173e", "outputId": "ccdf3391-69b7-47b1-d8ef-f6c6531ad5ae", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "24\n", "27\n", "25\n", "26\n" ] } ], "source": [ "for k in edad: # conseguimos listar el valor\n", " print(edad[k])" ] }, { "cell_type": "code", "execution_count": 14, "id": "58c41835", "metadata": { "id": "58c41835", "outputId": "7361abcd-aabf-4451-c790-97db202dfb3a", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Ana 24\n", "Luis 27\n", "Eva 25\n", "Raul 26\n" ] } ], "source": [ "for k in edad: # listamos ambos\n", " print(k, edad[k])" ] }, { "cell_type": "markdown", "id": "0ea901a4", "metadata": { "id": "0ea901a4" }, "source": [ "### Usando items" ] }, { "cell_type": "code", "execution_count": 15, "id": "0688d39a", "metadata": { "id": "0688d39a", "outputId": "73c70172-c1b2-4696-b3ce-96d5d793ddf5", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Ana 24\n", "Luis 27\n", "Eva 25\n", "Raul 26\n" ] } ], "source": [ "for k,v in edad.items():\n", " print(k, v)" ] } ], "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.8.8" }, "colab": { "name": "0240_diccionarios2.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }