Created
July 4, 2016 14:15
-
-
Save anonymous/49af198b82772510c70b69cde1630131 to your computer and use it in GitHub Desktop.
copy or view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Before:\n", | |
" x = [0 1 2 3 4]\n", | |
" y = 1\n", | |
"\n", | |
"After:\n", | |
" x = [0 1 2 3 4]\n", | |
" y = 2\n" | |
] | |
} | |
], | |
"source": [ | |
"# indexing by integer copies the value\n", | |
"\n", | |
"x = np.arange(5)\n", | |
"\n", | |
"# take second element\n", | |
"y = x[1]\n", | |
"\n", | |
"print \"Before:\"\n", | |
"print \" x =\", x\n", | |
"print \" y = \", y\n", | |
"print \n", | |
"\n", | |
"# change y in place\n", | |
"y+=1\n", | |
"\n", | |
"print \"After:\"\n", | |
"print \" x =\", x\n", | |
"print \" y = \", y" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Before:\n", | |
" x = [0 1 2 3 4]\n", | |
" y = [0 1]\n", | |
"\n", | |
"After:\n", | |
" x = [0 2 2 3 4]\n", | |
" y = [0 2]\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"# indexing by a slice creates a view\n", | |
"\n", | |
"x = np.arange(5)\n", | |
"\n", | |
"# take first two elements\n", | |
"y = x[:2]\n", | |
"\n", | |
"print \"Before:\"\n", | |
"print \" x =\", x\n", | |
"print \" y =\", y\n", | |
"print \n", | |
"\n", | |
"# change second element of y in place\n", | |
"y[1]+=1\n", | |
"\n", | |
"\n", | |
"print \"After:\"\n", | |
"print \" x =\", x\n", | |
"print \" y =\", y\n", | |
"print " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"\n" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.11" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment