{ "metadata": { "name": "", "signature": "sha256:7f0903be5b2382bace436f329a62545ec5a4735d167e80c1c2ee9f8bbcaf2988" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Data Structure Basics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- **Author:** [Chris Albon](http://www.chrisalbon.com/), [@ChrisAlbon](https://twitter.com/chrisalbon)\n", "- **Date:** -\n", "- **Repo:** [Python 3 code snippets for data science](https://github.com/chrisalbon/code_py)\n", "- **Note:**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are four: lists, truples, dictionaries, and sets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"A list is a data structure that holds an ordered collection of items i.e. you can store a sequence of items in a list.\" - A Byte Of Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists are mutable." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a list of countries, then print the results" ] }, { "cell_type": "code", "collapsed": false, "input": [ "allies = ['USA','UK','France','New Zealand',\n", " 'Australia','Canada','Poland']; allies" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 18, "text": [ "['USA', 'UK', 'France', 'New Zealand', 'Australia', 'Canada', 'Poland']" ] } ], "prompt_number": 18 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Print the length of the list" ] }, { "cell_type": "code", "collapsed": false, "input": [ "len(allies)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 19, "text": [ "7" ] } ], "prompt_number": 19 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add an item to the list, then print the results" ] }, { "cell_type": "code", "collapsed": false, "input": [ "allies.append('China'); allies" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 20, "text": [ "['USA',\n", " 'UK',\n", " 'France',\n", " 'New Zealand',\n", " 'Australia',\n", " 'Canada',\n", " 'Poland',\n", " 'China']" ] } ], "prompt_number": 20 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sort list, then print the results" ] }, { "cell_type": "code", "collapsed": false, "input": [ "allies.sort(); allies" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 21, "text": [ "['Australia',\n", " 'Canada',\n", " 'China',\n", " 'France',\n", " 'New Zealand',\n", " 'Poland',\n", " 'UK',\n", " 'USA']" ] } ], "prompt_number": 21 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Reverse sort list, then print the results" ] }, { "cell_type": "code", "collapsed": false, "input": [ "allies.sort(); allies" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 22, "text": [ "['Australia',\n", " 'Canada',\n", " 'China',\n", " 'France',\n", " 'New Zealand',\n", " 'Poland',\n", " 'UK',\n", " 'USA']" ] } ], "prompt_number": 22 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### View the first item of the list" ] }, { "cell_type": "code", "collapsed": false, "input": [ "allies[0]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 23, "text": [ "'Australia'" ] } ], "prompt_number": 23 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### View the last item of the list" ] }, { "cell_type": "code", "collapsed": false, "input": [ "allies[-1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 24, "text": [ "'USA'" ] } ], "prompt_number": 24 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Delete the item in the list" ] }, { "cell_type": "code", "collapsed": false, "input": [ "del allies[0]; allies" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 25, "text": [ "['Canada', 'China', 'France', 'New Zealand', 'Poland', 'UK', 'USA']" ] } ], "prompt_number": 25 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add a numeric value to a list of strings" ] }, { "cell_type": "code", "collapsed": false, "input": [ "allies.append(3442); allies" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 26, "text": [ "['Canada', 'China', 'France', 'New Zealand', 'Poland', 'UK', 'USA', 3442]" ] } ], "prompt_number": 26 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Truples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"Though tuples may seem similar to lists, they are often used in different situations and for different purposes. Tuples are immutable, and usually contain an heterogeneous sequence of elements that are accessed via unpacking (or indexing (or even by attribute in the case of namedtuples). Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.\" - Python Documentation\n", "\n", "\"Tuples are heterogeneous data structures (i.e., their entries have different meanings), while lists are homogeneous sequences.\" - StackOverflow" ] }, { "cell_type": "code", "collapsed": false, "input": [ "Parentheses are optional, but useful." ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "pyerr", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m Parentheses are optional, but useful.\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "prompt_number": 27 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a tuple of state names" ] }, { "cell_type": "code", "collapsed": false, "input": [ "usa = ('Texas', 'California', 'Maryland'); usa" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 28, "text": [ "('Texas', 'California', 'Maryland')" ] } ], "prompt_number": 28 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a tuple of countries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(notice the USA has a state names in the nested tuple)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "countries = ('canada', 'mexico', usa); countries" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 29, "text": [ "('canada', 'mexico', ('Texas', 'California', 'Maryland'))" ] } ], "prompt_number": 29 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### View the third item of the top tuple" ] }, { "cell_type": "code", "collapsed": false, "input": [ "countries[2]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 30, "text": [ "('Texas', 'California', 'Maryland')" ] } ], "prompt_number": 30 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### View the third item of the third tuple" ] }, { "cell_type": "code", "collapsed": false, "input": [ "countries[2][2]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 31, "text": [ "'Maryland'" ] } ], "prompt_number": 31 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionaries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"A dictionary is like an address-book where you can find the address or contact details of a person by knowing only his/her name i.e. we associate keys (name) with values (details). Note that the key must be unique just like you cannot find out the correct information if you have two persons with the exact same name.\" - A Byte Of Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a dictionary with key:value combos" ] }, { "cell_type": "code", "collapsed": false, "input": [ "staff = {'Chris' : 'chris@stater.org',\n", " 'Jake' : 'jake@stater.org',\n", " 'Ashley' : 'ashley@stater.org',\n", " 'Shelly' : 'shelly@stater.org'\n", " }" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 32 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Print the value using the key" ] }, { "cell_type": "code", "collapsed": false, "input": [ "staff['Chris']" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 33, "text": [ "'chris@stater.org'" ] } ], "prompt_number": 33 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Delete a dictionary entry based on the key" ] }, { "cell_type": "code", "collapsed": false, "input": [ "del staff['Chris']; staff" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 34, "text": [ "{'Jake': 'jake@stater.org',\n", " 'Ashley': 'ashley@stater.org',\n", " 'Shelly': 'shelly@stater.org'}" ] } ], "prompt_number": 34 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add an item to the dictionary" ] }, { "cell_type": "code", "collapsed": false, "input": [ "staff['Guido'] = 'guido@python.org'; staff" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 35, "text": [ "{'Jake': 'jake@stater.org',\n", " 'Ashley': 'ashley@stater.org',\n", " 'Guido': 'guido@python.org',\n", " 'Shelly': 'shelly@stater.org'}" ] } ], "prompt_number": 35 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sets are unordered collections of simple objects." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a set of BRI countries" ] }, { "cell_type": "code", "collapsed": false, "input": [ "BRI = set(['brazil', 'russia', 'india'])" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 52 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Is India in the set BRI?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "'india' in BRI" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 45, "text": [ "True" ] } ], "prompt_number": 45 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Is the US in the set BRI?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "'usa' in BRI" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 46, "text": [ "False" ] } ], "prompt_number": 46 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a copy of BRI called BRIC" ] }, { "cell_type": "code", "collapsed": false, "input": [ "BRIC = BRI.copy()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 47 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add China to BRIC" ] }, { "cell_type": "code", "collapsed": false, "input": [ "BRIC.add('china')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 48 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Is BRIC a super-set of BRI?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "BRIC.issuperset(BRI)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 49, "text": [ "True" ] } ], "prompt_number": 49 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Remove Russia from BRI" ] }, { "cell_type": "code", "collapsed": false, "input": [ "BRI.remove('russia')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 50 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### What items are the union of BRI and BRIC?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "BRI & BRIC" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 51, "text": [ "{'brazil', 'india'}" ] } ], "prompt_number": 51 } ], "metadata": {} } ] }