{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#Advanced Sets\n", "In this lecture we will learn about the various methods for sets that you may not have seen yet. We'll go over the basic ones you already know and then dive a little deeper." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s = set()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#add\n", "add elements to a set. Remember a set won't take duplicate elements and only present them once (thats why its called a set!)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s.add(1)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s.add(2)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 2}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##clear\n", "removes all elements from the set" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s.clear()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "set()" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##copy\n", "returns a copy of the set. Note it is a copy, so changes to the original don't effect the copy." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s = {1,2,3}\n", "sc = s.copy()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3}" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sc" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s.add(4)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4}" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3}" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##difference\n", "difference returns the difference of two or more sets. The syntax is:\n", "\n", " set1.difference(set2)\n", "For example:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{4}" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.difference(sc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##difference_update\n", "difference_update syntax is:\n", "\n", " set1.difference_update(set2)\n", "the method returns set1 after removing elements found in set2" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s1 = {1,2,3}" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s2 = {1,4,5}" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s1.difference_update(s2)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{2, 3}" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##discard\n", "Removes an element from a set if it is a member.If the element is not a member, do nothing." ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3}" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s.discard(2)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 3}" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##intersection and intersection_update\n", "Returns the intersection of two or more sets as a new set.(i.e. elements that are common to all of the sets.)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [], "source": [ "s1 = {1,2,3}" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [], "source": [ "s2 = {1,2,4}" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 2}" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1.intersection(s2)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3}" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "intersection_update will update a set with the intersection of itself and another." ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s1.intersection_update(s2)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 2}" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##isdisjoint\n", "This method will return True if two sets have a null intersection." ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": false }, "outputs": [], "source": [ "s1 = {1,2}\n", "s2 = {1,2,4}\n", "s3 = {5}" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1.isdisjoint(s2)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1.isdisjoint(s3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##issubset\n", "This method reports whether another set contains this set." ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 2}" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 4}" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s2" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1.issubset(s2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##issuperset\n", "This method will report whether this set contains another set." ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s2.issuperset(s1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##symmetric_difference and symmetric_update\n", "Return the symmetric difference of two sets as a new set.(i.e. all elements that are in exactly one of the sets.)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 2}" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 4}" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s2" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{4}" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1.symmetric_difference(s2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##union\n", "Returns the union of two sets (i.e. all elements that are in either set.)" ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 4}" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1.union(s2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##update\n", "Update a set with the union of itself and others." ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s1.update(s2)" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 4}" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Great! You should now have a complete awareness of all the methods available to you for a set object type. This data structure is extremely useful and is underutilized by beginners, so try to keep it in mind!\n", "\n", "Good Job!" ] } ], "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.10" } }, "nbformat": 4, "nbformat_minor": 0 }