{ "metadata": { "gist_id": "Error: No such file or directory - usrlocalnotebooksNo", "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lists\n", "\n", "* Put objects into a container;\n", "* labelled by position;\n", "* retrieve them by position, OR iterate over them." ] }, { "cell_type": "code", "collapsed": false, "input": [ "some_list = []\n", "some_list.append(5)\n", "some_list.append(6)\n", "some_list.append(7)\n", "\n", "print some_list\n", "for item in some_list:\n", " print item" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[5, 6, 7]\n", "5\n", "6\n", "7\n" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### HW 3.1" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Write Python code to calculate the average of the numbers in some_list; it should work for any set of numbers in some_list.\n", "print (5 + 6 + 7)/3.\n", "\n", "some_list=[1,2]\n", "\n", "print some_list\n", "\n", "total = 0.0\n", "for item in some_list:\n", " total = total + item\n", " \n", "average = total / len(some_list)\n", "print average\n", "\n", "print float(sum(some_list))/len(some_list)\n", "\n", "import numpy\n", "print numpy.mean(some_list)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "6.0\n", "[1, 2]\n", "1.5\n", "1.5\n", "1.5\n" ] } ], "prompt_number": 29 }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Dictionaries\n", "\n", "* put things into a container, labeled with a name;\n", "* retrieve them;\n", "* iterate over labels/names" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# for example, combining lists [] and dicts {} can be useful:\n", "letters_list = ['a', 'b', 'c', 'd', 'e']\n", "\n", "letters_dict = {}\n", "\n", "for i in range(len(letters_list)):\n", " single_letter = letters_list[i]\n", " letters_dict[single_letter] = i" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### HW 3.2" ] }, { "cell_type": "code", "collapsed": false, "input": [ "some_list = [\"duck\", \"goose\", \"mouse\", \"mouse\", \"mouse\", \"duck\"]\n", "\n", "counts = {}\n", "counts[\"duck\"] = 0\n", "\n", "for item in some_list:\n", " if item == \"duck\":\n", " counts[\"duck\"] += 1" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 31 }, { "cell_type": "code", "collapsed": false, "input": [ "## Write Python code to generically count the number of times *each* item in some_list is there.\n", "## The output should be in dictionary format, e.g. 'print counts[\"duck\"]' should show '2'.\n", "n_ducks = 0\n", "n_mice=0\n", "n_geese=0\n", "\n", "for item in some_list:\n", " if item == \"duck\":\n", " n_ducks += 1\n", "\n", "### ^^^ that's how you'd do it if you weren't using dictionaries\n", "\n", "counts = {}\n", "for item in some_list:\n", " counts[item] = 0\n", " \n", "for item in some_list:\n", " counts[item] = counts[item] + 1\n", " \n", "print counts" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'goose': 1, 'mouse': 3, 'duck': 2}\n" ] } ], "prompt_number": 39 }, { "cell_type": "code", "collapsed": false, "input": [ "# identical to ^^^\n", "counts = {}\n", "for item in some_list:\n", " counts[item] = counts.get(item, 0) + 1\n", " \n", "print counts" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'goose': 1, 'mouse': 3, 'duck': 2}\n" ] } ], "prompt_number": 38 }, { "cell_type": "code", "collapsed": false, "input": [ "counts = {}\n", "for item in some_list:\n", " n = 0\n", " for item2 in some_list:\n", " if item == item2:\n", " n += 1\n", " counts[item] = n\n", " # equivalent to\n", " #counts[item] = some_list.count(item)\n", "print counts" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'goose': 1, 'mouse': 3, 'duck': 2}\n" ] } ], "prompt_number": 41 }, { "cell_type": "code", "collapsed": false, "input": [ "s = \"ATGGACCAGATGGACACAGATTAGAGAGA\"\n", "\n", "counts = {}\n", "for ch in s:\n", " print 'at', ch, 'counts is', counts.get(ch, 0)\n", " counts[ch] = counts.get(ch, 0) + 1\n", "print counts\n", "\n", "counts2 = {}\n", "K = 4\n", "for i in range(len(s) - K + 1):\n", " word = s[i:i+K]\n", " counts2[word] = counts2.get(word, 0) + 1\n", "print counts2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "at A counts is 0\n", "at T counts is 0\n", "at G counts is 0\n", "at G counts is 1\n", "at A counts is 1\n", "at C counts is 0\n", "at C counts is 1\n", "at A counts is 2\n", "at G counts is 2\n", "at A counts is 3\n", "at T counts is 1\n", "at G counts is 3\n", "at G counts is 4\n", "at A counts is 4\n", "at C counts is 2\n", "at A counts is 5\n", "at C counts is 3\n", "at A counts is 6\n", "at G counts is 5\n", "at A counts is 7\n", "at T counts is 2\n", "at T counts is 3\n", "at A counts is 8\n", "at G counts is 6\n", "at A counts is 9\n", "at G counts is 7\n", "at A counts is 10\n", "at G counts is 8\n", "at A counts is 11\n", "{'A': 12, 'C': 4, 'T': 4, 'G': 9}\n", "{'ACAG': 1, 'GGAC': 2, 'ACAC': 1, 'ATGG': 2, 'TTAG': 1, 'TGGA': 2, 'GATT': 1, 'GAGA': 2, 'ACCA': 1, 'GACA': 1, 'CACA': 1, 'TAGA': 1, 'ATTA': 1, 'AGAT': 2, 'CAGA': 2, 'AGAG': 2, 'GACC': 1, 'GATG': 1, 'CCAG': 1}\n" ] } ], "prompt_number": 37 }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Functions\n", "\n", "Functions encapsulate code to make it reusable by making it generic and labelling it." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def add_two_numbers(a, b):\n", " c = a + b\n", " return c\n", "\n", "add_two_numbers(5, 10)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### HW 3.3\n", "\n", "Write functions for one or both of the above exercises; e.g." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# make the below code work by defining a new function, 'average'\n", "\n", "def average(the_list):\n", " total = 0.0\n", " for item in the_list:\n", " total = total + item\n", " \n", " result = total / len(the_list)\n", " return result" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 56 }, { "cell_type": "code", "collapsed": false, "input": [ "some_list = []\n", "some_list.append(5)\n", "some_list.append(6)\n", "some_list.append(7)\n", "print average(some_list)\n", "\n", "some_list" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "8.25\n" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 55, "text": [ "[5, 6, 7, 15]" ] } ], "prompt_number": 55 }, { "cell_type": "code", "collapsed": false, "input": [ "def count_items(the_list):\n", " counts = {}\n", " for item in some_list:\n", " counts[item] = 0\n", " \n", " for item in some_list:\n", " counts[item] = counts[item] + 1\n", " return counts" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 57 }, { "cell_type": "code", "collapsed": false, "input": [ "some_list = [\"duck\", \"goose\", \"mouse\", \"mouse\", \"mouse\", \"duck\"]\n", "\n", "counts_dict = count_items(some_list)\n", "print counts_dict\n", "print \"duck shows up\", counts_dict[\"duck\"], \"times\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'goose': 1, 'mouse': 3, 'duck': 2}\n", "duck shows up 2 times\n" ] } ], "prompt_number": 59 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Upload your homework\n", "\n", "When you're done, save the file and run the following cell. It will give you a URL like 'https://gist.github.com/7036805'. Take the number at the end and go to 'http://nbviewer.ipython.org' and enter it into the box; hit enter. You should see a static version of your notebook. Send me that URL as part of your homework hand-in (e.g. http://nbviewer.ipython.org/7036805)." ] }, { "cell_type": "code", "collapsed": true, "input": [ "!gist class3-lists-dicts-functions.ipynb" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "https://gist.github.com/7036800\r\n" ] } ], "prompt_number": 4 } ], "metadata": {} } ] }