{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "authorship_tag": "ABX9TyPa0RYmFbWz9sq4BQg40roB", "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": [ "\"Open" ] }, { "cell_type": "markdown", "source": [ "# Listas: copia superficial y copia profunda" ], "metadata": { "id": "lMlQ-s9GfQuu" } }, { "cell_type": "markdown", "source": [ "## Copiando una variable numérica\n", "* Al copiar una variable haciendo `y = x` hacemos el que el identificador de `y` sea el mismo que el de `x`, ambas apuntan a la misma dirección de memoria.\n", "* Si reasignamos el valor de una de ellas el valor de la otra no se altera, son independientes.\n", "* Para comprobar que son idependientes pedimos el `id` de ambas y vemos que ahora es diferente." ], "metadata": { "id": "L-5XOwo-fW1x" } }, { "cell_type": "code", "source": [ "x = 1\n", "y = 2\n", "\n", "print(id(x)) # Output: 140040150450416\n", "print(id(y)) # Output: 140040150450448\n", "\n", "y = x # realizamos una copia de una variable en otra\n", "\n", "print(id(x)) # Output: 140040150450416\n", "print(id(y)) # Output: 140040150450416\n", "\n", "x = 100 # reasignamos el valor de x\n", "\n", "print(x) # Output: 100\n", "print(y) # Output: 1\n", "\n", "print(id(x)) # Output: 140040150453584\n", "print(id(y)) # Output: 140040150450416" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "fUrgOmiA3JsF", "outputId": "b1d68cba-ae0c-48fe-b2ae-6552f4d7c5f1" }, "execution_count": 1, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "134714609271024\n", "134714609271056\n", "134714609271024\n", "134714609271024\n", "100\n", "1\n", "134714609274192\n", "134714609271024\n" ] } ] }, { "cell_type": "markdown", "source": [ "## Copia superficial: reasignando un elemento de la lista el `id` no cambia\n", "* Realizamos una copia superficial de una lista escribiendo `y = x`\n", "* Para realizar una copia profunda deberíamos haber empleando uno de estos métodos:\n", "- `y = copy(x)`\n", "- `y = list(x)`\n", "- y = x[:]" ], "metadata": { "id": "5mj15NAchsmp" } }, { "cell_type": "code", "source": [ "x = [1, 2, 3]\n", "\n", "y = x # COPIA SUPERFICIAL\n", "\n", "print(id(x)) # Output: 140039729902208\n", "print(id(y)) # Output: 140039729902208\n", "\n", "x[0] = 100 # reasignación del valor del un elemento del array\n", "\n", "print(x) # Output: [100, 2, 3]\n", "print(y) # Output: [100, 2, 3] tambien cambia el valor\n", "\n", "print(id(x)) # Output: 140039729902208\n", "print(id(y)) # Output: 140039729902208" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "1Yzk6YJk3opp", "outputId": "57b22640-aaaa-45af-f1dc-755137b164c3" }, "execution_count": 2, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "134714180775680\n", "134714180775680\n", "[100, 2, 3]\n", "[100, 2, 3]\n", "134714180775680\n", "134714180775680\n" ] } ] }, { "cell_type": "markdown", "source": [ "## Copia profunda de una lista" ], "metadata": { "id": "AyPIUIDzjqIR" } }, { "cell_type": "code", "source": [ "from copy import copy\n", "\n", "x = [1, 2, 3]\n", "\n", "y = copy(x) # COPIA PROFUNDA\n", "\n", "print(id(x)) # Output: 140039743881408\n", "print(id(y)) # Output: 140039692347264\n", "\n", "x[0] = 100 # reasignación del valor del un elemento del array\n", "\n", "print(x) # Output: [100, 2, 3]\n", "print(y) # Output: [1, 2, 3] no se altera el valor, es independiente\n", "\n", "print(id(x)) # Output: 140039743881408\n", "print(id(y)) # Output: 140039692347264" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "C7_jyIlFjld7", "outputId": "fb968453-2692-4642-bf9b-7afb8f47580c" }, "execution_count": 3, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "134714184138944\n", "134714180957120\n", "[100, 2, 3]\n", "[1, 2, 3]\n", "134714184138944\n", "134714180957120\n" ] } ] }, { "cell_type": "markdown", "source": [ "## Copia superficial: reasignando la lista entera el `id` cambia\n", "Si se reasigna completamente la lista igualando al variable a otra lista diferente vemos que el `id` cambia ya que se trata de una nueva lista y ya no está vinculada a la anteriro, no ocupan el mismo espacio en memoria.\n", "* Esto nos permite comprobar que la listas con copia superficial no equivalen al concepto de puntero en otros lenguajes, ya que a priori pudiera parecer que la copia superficial de una lista se comporta como un puntero, pero esta similitud se da únicamente cuando reasignamos algún elemento de la lista pero no cuando reasignamos la lista completa." ], "metadata": { "id": "dZxnUDpnktpr" } }, { "cell_type": "code", "source": [ "x = [1, 2, 3]\n", "\n", "y = x # COPIA SUPERFICIAL\n", "\n", "print(id(x)) # Output: 140039692436224\n", "print(id(y)) # Output: 140039692436224\n", "\n", "x = [100, 200, 200] # reasignamos completamente el valor de la lista\n", "\n", "print(x) # Output: [100, 200, 200]\n", "print(y) # Output: [1, 2, 3]\n", "\n", "print(id(x)) # Output: 140038894863616\n", "print(id(y)) # Output: 140039692436224" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "DzQG2ltA5D63", "outputId": "39b31283-518a-4279-ff28-7a9050394ac3" }, "execution_count": 4, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "134714180766464\n", "134714180766464\n", "[100, 200, 200]\n", "[1, 2, 3]\n", "134714184138944\n", "134714180766464\n" ] } ] }, { "cell_type": "markdown", "source": [ "## Alcance de las listas\n", "Otro motivo por el que una lista con copia superficial se comporta de forma similar a un puntero lo podemos ver en el alcance de las variables locales dentro de un función." ], "metadata": { "id": "uyK5ES0N-1t2" } }, { "cell_type": "code", "source": [ "lista = [1,2,3]\n", "print(f\"La lista externa es {lista}\\n\")\n", "\n", "def modifica(lista):\n", " lista[0] = 100\n", " print(f\"La lista interna es {lista}\")\n", "\n", "print(\"Ejecutamos la función que modifica la lista\\n\")\n", "modifica(lista)\n", "print(f\"La lista externa es {lista}\")" ], "metadata": { "id": "H7LAhNa3_F56", "outputId": "458c160c-72b6-41ed-9be6-ae0dc53ebb72", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": 5, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "La lista externa es [1, 2, 3]\n", "\n", "Ejecutamos la función que modifica la lista\n", "\n", "La lista interna es [100, 2, 3]\n", "La lista externa es [100, 2, 3]\n" ] } ] } ] }