{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# operations\n", "\n", "# copy a list without affecting original b=a[:] \n", "\n", "- otherwise it is a pointer\n", "\n", "\n", "# list comprehension" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## simplest example" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1, 2, 3, 4, 5]\n", "a" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = list(range(1,6))\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## list \n", " -l.append \n", " -l.clear \n", " -l.copy \n", " -l.count \n", " -l.extend \n", " -l.index \n", " -l.insert \n", " -l.pop \n", " -l.remove \n", " -l.reverse" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6]" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.append(6)\n", "a" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#note that .pop gives the one popped out, default is the last one, but you can give it a position to pop\n", "a.pop()" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a.append(6)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### count how many times a member repeats" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.count(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### simple stats" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max(a)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "15" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### sorting is permanent!" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a.sort(reverse=True)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[5, 4, 3, 2, 1]" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#let's get it back to what it was\n", "a.sort()\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## copy a list (*danger zone*)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## b=a[:]\n", " copying without affecting the original list " ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b=a[:]\n", "b" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 100]" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.append(100)\n", "b" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b==a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## b=a\n", " copying by pointing at the original list \n", "\n", "### * danger! potential bug area!!!*" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": true }, "outputs": [], "source": [ "b=a" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 100, 100, 100]" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.append(100)\n", "b" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 100, 100, 100]" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## OMG!" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 100, 100, 100, 100, 100]" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b=a\n", "b.append(100)\n", "a\n", "b=a\n", "b.append(100)\n", "a\n", "b=a\n", "b.append(100)\n", "a\n", "b=a\n", "b.append(100)\n", "a\n", "b=a\n", "b.append(100)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## list comprehensions" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 2, 9, 4, 9]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1, 2, 3, 4, 5]\n", "b = [2, 2, 9, 0, 9]\n", "def pick_the_larger(x,y):\n", " result = [] # A list of the largest values\n", "\n", " # Assume both lists are the same length\n", " list_length = len(x)\n", " for i in range(list_length):\n", " result.append(max(x[i], y[i]))\n", " return result\n", "pick_the_larger(a,b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### map\n", "*map(some_function, some_iterable)*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### pythonic code - list comprehensions \n", "Faster performance: the less helper functions and the more language features" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 2, 9, 4, 9]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(map(max, zip(a,b)))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 2, 9, 4, 9]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[max(pair) for pair in zip(a,b)]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 2, 9, 4, 9]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[max(ai,bi) for ai, bi in zip(a,b)]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 2, 9, 4, 9]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# in this case, max works on iterable too\n", "list(map(max,a,b))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 2, 9, 4, 9]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[max(a[x],b[x]) for x in range(len(a))]" ] } ], "metadata": { "kernelspec": { "display_name": "Python [default]", "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.3" } }, "nbformat": 4, "nbformat_minor": 2 }