Skip to content

Instantly share code, notes, and snippets.

@jonathan-taylor
Last active November 14, 2019 00:31
Show Gist options
  • Save jonathan-taylor/13dbbbf65c6a06d0b0cc9a6d6a1f4ab2 to your computer and use it in GitHub Desktop.
Save jonathan-taylor/13dbbbf65c6a06d0b0cc9a6d6a1f4ab2 to your computer and use it in GitHub Desktop.
Copying a list
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np, pandas as pd\n",
"import copy\n",
"\n",
"X = np.random.standard_normal(5)\n",
"D = pd.DataFrame({'Y': np.random.standard_normal(10)})\n",
"\n",
"original = {'X':X, 'D':D}\n",
"new = copy.copy(original)\n",
"notacopy = original\n",
"notacopy['X'] = np.random.standard_normal(5)\n",
"new['X'] = np.random.standard_normal(5)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[-0.49819521 -0.05616186 -0.47594206 -0.18163103 1.07650505] [ 1.63738458 -0.64480777 0.21228982 -1.03785832 0.97276819] [ 1.63738458 -0.64480777 0.21228982 -1.03785832 0.97276819]\n",
"4792221248 4738445920 4738445920\n"
]
}
],
"source": [
"print(new['X'], original['X'], notacopy['X'])\n",
"print(id(new['X']), id(original['X']), id(notacopy['X']))"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Y\n",
"0 -0.612042\n",
"1 -1.125589\n",
"2 -0.215406\n",
"3 0.206762\n",
"4 0.037567\n",
"5 0.509506\n",
"6 -0.755514\n",
"7 -0.781018\n",
"8 -1.031885\n",
"9 0.709235\n",
" Y\n",
"0 -0.612042\n",
"1 -1.125589\n",
"2 -0.215406\n",
"3 0.206762\n",
"4 0.037567\n",
"5 0.509506\n",
"6 -0.755514\n",
"7 -0.781018\n",
"8 -1.031885\n",
"9 0.709235\n",
" Y\n",
"0 -0.612042\n",
"1 -1.125589\n",
"2 -0.215406\n",
"3 0.206762\n",
"4 0.037567\n",
"5 0.509506\n",
"6 -0.755514\n",
"7 -0.781018\n",
"8 -1.031885\n",
"9 0.709235\n"
]
},
{
"data": {
"text/plain": [
"(4792506464, 4792506464, 4792506464)"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(new['D'])\n",
"print(original['D'])\n",
"print(notacopy['D'])\n",
"id(new['D']), id(original['D']), id(notacopy['D'])"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The rpy2.ipython extension is already loaded. To reload it, use:\n",
" %reload_ext rpy2.ipython\n"
]
}
],
"source": [
"%load_ext rpy2.ipython"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] 0.4928976 -0.4469674 0.8852140 -0.7353284 -0.8700456\n",
"[1] -1.8172759 0.1827414 -0.9300616 -1.6686778 0.7318398\n",
"[1] -0.09017105 -0.06625104 1.12459441 -0.49675068 -0.16255790\n",
"[1] -0.09017105 -0.06625104 1.12459441 -0.49675068 -0.16255790\n",
"[1] \"0x7f8ea14618d8\"\n",
"[1] \"0x7f8ea14618d8\"\n",
"[1] \"0x7f8ea1461940\"\n",
"[1] \"0x7f8ea1461870\"\n"
]
}
],
"source": [
"%%R\n",
"library(pryr)\n",
"original = list(X=rnorm(5), Y=rnorm(5))\n",
"new = original\n",
"new[['X']] = rnorm(5)\n",
"print(new[['X']])\n",
"print(original[['X']])\n",
"print(new[['Y']])\n",
"print(original[['Y']])\n",
"W = new[['Y']]\n",
"print(pryr::address(W))\n",
"W = original[['Y']]\n",
"print(pryr::address(W))\n",
"W = new[['X']]\n",
"print(pryr::address(W))\n",
"W = original[['X']]\n",
"print(pryr::address(W))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"jupytext": {
"cell_metadata_filter": "all,-slideshow"
},
"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.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment