Skip to content

Instantly share code, notes, and snippets.

@GEJ1
Created December 23, 2021 15:10
Show Gist options
  • Save GEJ1/6bb0a3f81e3c1bc183c12f3058fc4557 to your computer and use it in GitHub Desktop.
Save GEJ1/6bb0a3f81e3c1bc183c12f3058fc4557 to your computer and use it in GitHub Desktop.
comprehensions.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "comprehensions.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyNdvvGfs/URG34X0iSgHw4O",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/GEJ1/6bb0a3f81e3c1bc183c12f3058fc4557/comprehensions.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
"######## Listas por comprensión ########\n",
"# [<expresion> for <item> in <iterable> if <condicion>]\n",
"\n",
"lista1 = [1,2,3,4,5,6,7,8]\n",
"lista2 = [ item * 2 for item in lista1 if item > 5]\n",
"print(lista2)\n",
"# >> [12, 14, 16]"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "LIgbJACsTtcD",
"outputId": "98c52154-189d-4507-a3b5-08479f6823df"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[12, 14, 16]\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"######## Diccionario por comprensión ########\n",
"# {<clave>:<valor> for <item> in <iterable> if <condicion>}\n",
"\n",
"nombres = ['Carla', 'Ramiro', 'Erica']\n",
"profesion = ['Ingeniera', 'Psicólogo', 'Empresaria']\n",
"\n",
"mi_diccionario = {clave:valor for (clave,valor) in zip(nombres,profesion)}\n",
"print(mi_diccionario)\n",
"# >> {'Carla': 'Ingeniera', 'Ramiro': 'Psicólogo', 'Erica': 'Empresaria'}"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "BY5jM8mgUzaF",
"outputId": "2c09395d-ae1f-4bfc-e856-8708fb66048e"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"{'Carla': 'Ingeniera', 'Ramiro': 'Psicólogo', 'Erica': 'Empresaria'}\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"######## Sets (conjuntos) por comprensión ########\n",
"# {<expression> for <item> in <iterable> if <condition>} \n",
"# Ojo: La diferencia con los diccionarios es que aca no hay clave y valor\n",
"\n",
"lista = ['perro', 'gato', 'mono', 'pelicano']\n",
"\n",
"mi_set = {item for item in lista if item.startswith('pe')} \n",
"print(mi_set)\n",
"# >> {'perro', 'pelicano'}"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "NVephwkTZBZF",
"outputId": "e388024c-3d0e-4381-913d-a34cd9ef3a59"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"{'perro', 'pelicano'}\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"######## Generadores por comprensión ########\n",
"# (<expression> for <item> in <iterable> if <condition>)\n",
"# Notar que no necesitamos la palabra reservada 'yield'\n",
"\n",
"lista = [2, 4, 6, 8, 10]\n",
"mi_generador = (numero**2 for numero in lista)\n",
"print(next(mi_generador))\n",
"print(next(mi_generador))\n",
"# >> 4\n",
"# >> 16"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "WY1Zi1yLXmd0",
"outputId": "cf0c4d5b-8231-4cd8-80fe-1385d74eb180"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"4\n",
"16\n"
]
}
]
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "bW7fSrkZao1s"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment