{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Notes on data structures\n", "\n", "#### Again, credit to Mariela Perignon\n", "\n", "Data which can be modified in place is called mutable, while data which cannot be modified is called immutable. Strings and numbers are immutable. This does not mean that variables with string or number values are constants, but when we want to change the value of a string or number variable, we can only replace the old value with a completely new value." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = 'a'\n", "a = 'b'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists and arrays, on the other hand, are mutable: we can modify them after they have been created. We can change individual elements, append new elements, or reorder the whole list. For some operations, like sorting, we can choose whether to use a function that modifies the data in place or a function that returns a modified copy and leaves the original unchanged." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 3, 10, 8, 4, 2]\n", "[1, 2, 3, 4, 8, 10]\n", "[1, 2, 3, 4, 8, 10]\n" ] } ], "source": [ "a = [1,3,10,8,4,2]\n", "print(a)\n", "\n", "b = sorted(a) #doesn't change a; b is sorted a\n", "print(a)\n", "print(b)\n", "\n", "a = sorted(a) #changes a by reassigning the returned list to a\n", "print(a)\n", "\n", "a = [1,3,10,8,4,2]\n", "print(a)\n", "a.sort() #sorts a in place without having to reassign\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Be careful when modifying data in place. If two variables refer to the same list, and you modify the list value, it will change for both variables! If you want variables with mutable values to be independent, you must make a copy of the value when you assign it." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 8, 10]\n", "[1, 2, 3, 4, 8, 10]\n" ] } ], "source": [ "a = [1,3,10,8,4,2]\n", "b = a #b and a refer to the same data\n", "a.sort() #changing a in place also changes b because it's the same data\n", "print(a)\n", "print(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " If we want to really copy a list, we can use the list() command to clone it and create a new and independent list." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 8, 10]\n", "[1, 3, 10, 8, 4, 2]\n" ] } ], "source": [ "a = [1,3,10,8,4,2]\n", "b = list(a) #b and a refer to the same data\n", "a.sort() #changing a in place doesn't change b because it's a real copy\n", "print(a)\n", "print(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Because of pitfalls like this, code which modifies data in place can be more difficult to understand. However, it is often far more efficient to modify a large data structure in place than to create a modified copy for every small change. You should consider both of these aspects when writing your code.\n", "\n", "Here are some additional ways to modify a list in place." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a after adding a value: [1, 3, 10, 8, 4, 2, 11]\n", "a after removing the first element: [3, 10, 8, 4, 2, 11]\n", "a after reversing: [11, 2, 4, 8, 10, 3]\n" ] } ], "source": [ "a = [1,3,10,8,4,2]\n", "a.append(11)\n", "print('a after adding a value:', a)\n", "\n", "a.pop(0)\n", "print('a after removing the first element:', a)\n", "\n", "a.reverse()\n", "print('a after reversing:', a)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also convert between data types" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['h', 'e', 'l', 'l', 'o']\n", "hello\n", "15\n", "15\n" ] } ], "source": [ "#Convert a string to a list\n", "print(list('hello'))\n", "\n", "#join together the items in a list with sep = item between ''\n", "print(''.join(['h','e','l','l','o']))\n", "\n", "#convert a string to an integer\n", "print(int('15'))\n", "\n", "#convert an integer to a string\n", "print(str(15))" ] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:py36]", "language": "python", "name": "conda-env-py36-py" }, "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.1" } }, "nbformat": 4, "nbformat_minor": 1 }