{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "IvWewTdu_-XU" }, "source": [ "# Conjuntos con estructuras\n", "Uso de bucles en conjuntos (```set```)" ], "id": "IvWewTdu_-XU" }, { "cell_type": "markdown", "metadata": { "id": "wyJ8T_Dx_-XU" }, "source": [ "Disponemos del conjunto frutas y del conjunto cítricos. \n", "Deseamos un listado de todas las frutas, realizado con un *for*, indicando si pertenecen al conjunto cítricos." ], "id": "wyJ8T_Dx_-XU" }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "zE5KYnJ7_-XV" }, "outputs": [], "source": [ "frutas = {'Uva', 'Naranja', 'Melón', 'Pomelo', 'Manzana', 'Pera', 'Limón' }\n", "citricos =set(('Pomelo', 'Naranja', 'Limón'))" ], "id": "zE5KYnJ7_-XV" }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "-XY9jt5z_-XV", "outputId": "b14c1654-6b4b-49f6-fa26-1eb9374132b2", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'Limón', 'Manzana', 'Melón', 'Naranja', 'Pera', 'Pomelo', 'Uva'}" ] }, "metadata": {}, "execution_count": 2 } ], "source": [ "frutas" ], "id": "-XY9jt5z_-XV" }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "9VJZHsZM_-XW", "outputId": "9b9d3ac4-1092-4705-ec32-2dc95a8c35e9", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'Limón', 'Naranja', 'Pomelo'}" ] }, "metadata": {}, "execution_count": 3 } ], "source": [ "citricos" ], "id": "9VJZHsZM_-XW" }, { "cell_type": "code", "execution_count": 4, "metadata": { "id": "t_JfK-yw_-XW", "outputId": "cde03f0e-cfcc-4059-e39c-bce6b842141d", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Naranja → es un cítrico\n", "Pomelo → es un cítrico\n", "Manzana\n", "Pera\n", "Melón\n", "Uva\n", "Limón → es un cítrico\n" ] } ], "source": [ "for fruta in frutas:\n", " if fruta in citricos:\n", " print(f\"{fruta} → es un cítrico\")\n", " else:\n", " print(fruta)" ], "id": "t_JfK-yw_-XW" }, { "cell_type": "markdown", "metadata": { "id": "fTu8Wj9c_-XW" }, "source": [ "## set comprehension" ], "id": "fTu8Wj9c_-XW" }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "UDw7lHcF_-XW", "outputId": "549b68b5-28f7-4d90-b1a4-d932f700a041", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{1, 4, 9, 16, 25}" ] }, "metadata": {}, "execution_count": 5 } ], "source": [ "{i**2 for i in [1, 2, 3, 4, 5]}" ], "id": "UDw7lHcF_-XW" }, { "cell_type": "code", "execution_count": 6, "metadata": { "id": "r9iqGOr6_-XX", "outputId": "526957e3-ed98-4adb-dd8d-39dec41bac42", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{1, 3, 5, 7, 9}" ] }, "metadata": {}, "execution_count": 6 } ], "source": [ "{i for i in range(10) if i % 2} # conjunto de impares" ], "id": "r9iqGOr6_-XX" }, { "cell_type": "markdown", "metadata": { "id": "0c68OXmh_-XX" }, "source": [ "{(m, n) for n in range(2) for m in range(3, 5)}" ], "id": "0c68OXmh_-XX" }, { "cell_type": "code", "execution_count": 7, "metadata": { "id": "8yUSwiG5_-XX", "outputId": "f4f728d7-e640-4c7d-aaeb-c87daa5ee4bf", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{(0, 10), (0, 11), (0, 12), (0, 13), (1, 10), (1, 11), (1, 12), (1, 13)}" ] }, "metadata": {}, "execution_count": 7 } ], "source": [ "from random import randint\n", "{(i,j) for i in range(2) for j in range(10,14)}" ], "id": "8yUSwiG5_-XX" }, { "cell_type": "code", "execution_count": 8, "metadata": { "id": "vhWChQRF_-XX", "outputId": "6cd5666e-d8d0-4025-9128-6bea232265c1", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{' ', 'n', 's', 't'}" ] }, "metadata": {}, "execution_count": 8 } ], "source": [ "{c for c in 'amigo es algo cierto o solo un rumor ' if c not in 'murcielago'}" ], "id": "vhWChQRF_-XX" }, { "cell_type": "markdown", "metadata": { "id": "kohbbHTZ_-XX" }, "source": [ "## if all, if not all" ], "id": "kohbbHTZ_-XX" }, { "cell_type": "code", "execution_count": 9, "metadata": { "id": "iKf2JLWY_-XY", "outputId": "26d874ba-19c8-4a90-f584-54fcec30bfa4", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 9 } ], "source": [ "all({i in \"amor\" for i in \"roma\"}) # para detectar anagramas, armonización – romanización" ], "id": "iKf2JLWY_-XY" } ], "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.5" }, "colab": { "name": "0430_conjuntos_con_estructuras.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }