{"id":87,"date":"2014-12-08T16:00:57","date_gmt":"2014-12-08T21:00:57","guid":{"rendered":"http:\/\/pfsensesetup.com\/pythonscript.net\/?p=87"},"modified":"2014-12-08T11:27:58","modified_gmt":"2014-12-08T16:27:58","slug":"python-sets-part-one","status":"publish","type":"post","link":"http:\/\/pfsensesetup.com\/pythonscript.net\/python-sets-part-one\/","title":{"rendered":"Python Sets: Part One"},"content":{"rendered":"<div id=\"attachment_88\" style=\"width: 310px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-content\/uploads\/2014\/12\/LEGO-Star-Wars-Battle-Pack.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-88\" class=\"size-medium wp-image-88\" src=\"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-content\/uploads\/2014\/12\/LEGO-Star-Wars-Battle-Pack-300x260.jpg\" alt=\"Python sets\" width=\"300\" height=\"260\" srcset=\"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-content\/uploads\/2014\/12\/LEGO-Star-Wars-Battle-Pack-300x260.jpg 300w, http:\/\/pfsensesetup.com\/pythonscript.net\/wp-content\/uploads\/2014\/12\/LEGO-Star-Wars-Battle-Pack.jpg 500w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-88\" class=\"wp-caption-text\">No, not that kind of set.<\/p><\/div>\n<p>One of the core Python data types that we did not mention in earlier articles that perhaps deserves some attention is the set. Sets are a recent addition to Python; they are neither mappings (dictionaries) nor sequences (strings, lists and typles). Sets are created by calling the built-in set function or using new set literals and expressions in 3.0, and they support the usual mathematical set operations.<\/p>\n<h3><span style=\"text-decoration: underline;\"><strong>Introduction to Sets<\/strong><\/span><\/h3>\n<p>Creating a set can be done two different ways:<\/p>\n<p><strong>&gt;&gt;&gt; mySet = set(&#8216;hello&#8217;)<\/strong><br \/>\n<strong> &gt;&gt;&gt; otherSet = {&#8216;a&#8217;,&#8217;b&#8217;,&#8217;c&#8217;,&#8217;d&#8217;,&#8217;e&#8217;}<\/strong><\/p>\n<p>len returns the number of unique set items, so we get:<br \/>\n<strong>&gt;&gt;&gt; len(otherSet)<\/strong><br \/>\n<strong> 5<\/strong><br \/>\nBut if we run the same operation on mySet, we get:<br \/>\n<strong>&gt;&gt;&gt; len(mySet)<\/strong><br \/>\n<strong> 4<\/strong><\/p>\n<p><strong>&gt;&gt;&gt; &#8216;h&#8217; in mySet<\/strong><br \/>\n<strong> True<\/strong><br \/>\n<strong> &gt;&gt;&gt; &#8216;i&#8217; in mySet<\/strong><br \/>\n<strong> False<\/strong><br \/>\n<strong> &gt;&gt;&gt; mySet.isdisjoint(otherSet)<\/strong><br \/>\n<strong> False<\/strong><br \/>\n<strong> &gt;&gt;&gt; newSet = set(&#8216;he&#8217;)<\/strong><br \/>\n<strong> newSet.issubset(mySet)<\/strong><br \/>\n<strong> True<\/strong><\/p>\n<p>Mathematical set operations are generally valid on sets:<\/p>\n<p><strong>&gt;&gt;&gt; mySet, otherSet<\/strong><br \/>\n<strong> ({&#8216;o&#8217;, &#8216;e&#8217;, &#8216;l&#8217;, &#8216;h&#8217;}, {&#8216;e&#8217;, &#8216;d&#8217;, &#8216;b&#8217;, &#8216;a&#8217;, &#8216;c&#8217;})<\/strong><\/p>\n<p><strong>&gt;&gt;&gt; mySet &amp; otherSet<\/strong><br \/>\n<strong> {&#8216;e&#8217;}<\/strong><\/p>\n<p><strong>&gt;&gt;&gt; mySet | otherSet<\/strong><br \/>\n<strong> {&#8216;e&#8217;, &#8216;h&#8217;, &#8216;o&#8217;, &#8216;d&#8217;, &#8216;b&#8217;, &#8216;c&#8217;, &#8216;a&#8217;, &#8216;l&#8217;}<\/strong><br \/>\n<strong> &gt;&gt;&gt; mySet &#8211; otherSet<\/strong><br \/>\n<strong> {&#8216;o&#8217;, &#8216;l&#8217;, &#8216;h&#8217;}<\/strong><\/p>\n<p>There are a few other operations. &lt;= tests whether ever element in the left operand set is in the right operand set. For example: &gt;&gt;&gt; newSet &lt;= mySet<br \/>\nTrue<\/p>\n<p>But what if we want to return False if the sets are equal? Then we use &lt;:<br \/>\n<strong>&gt;&gt; newSet &lt; mySet<\/strong><\/p>\n<p><strong>True<\/strong><\/p>\n<p><strong>&gt;&gt;&gt; exactcopy = set(mySet)<\/strong><br \/>\n<strong> &gt;&gt;&gt; exactcopy &lt; mySet<\/strong><\/p>\n<p><strong>False<\/strong><\/p>\n<p><strong>&gt;&gt;&gt; exactcopy &lt;= mySet<\/strong><\/p>\n<p><strong>True<\/strong><\/p>\n<p>We can flip the operand around, and check to see if the left operand set is a superset of the right operand set:<\/p>\n<p><strong>&gt;&gt;&gt; mySet &gt; newSet<\/strong><br \/>\n<strong> True<\/strong><br \/>\n<strong> &gt;&gt;&gt; mySet &gt;= newSet<\/strong><br \/>\n<strong> True<\/strong><\/p>\n<p>Sets are mutable; you can add and remove items with the add and remove methods:<\/p>\n<p><strong>&gt;&gt;&gt; otherSet.add(&#8216;f&#8217;)<\/strong><br \/>\n<strong> {&#8216;e&#8217;, &#8216;f&#8217;, &#8216;d&#8217;, &#8216;b&#8217;, &#8216;c&#8217;, &#8216;a&#8217;}<\/strong><br \/>\n<strong> &gt;&gt;&gt; otherSet.remove(&#8216;c&#8217;)<\/strong><br \/>\n<strong> &gt;&gt;&gt; otherSet<\/strong><br \/>\n<strong> {&#8216;e&#8217;, &#8216;f&#8217;, &#8216;d&#8217;, &#8216;b&#8217;, &#8216;a&#8217;}<\/strong><\/p>\n<p>You can also iterate over a set. For example:<br \/>\n<strong>&gt;&gt;&gt; for s in mySet:<\/strong><br \/>\n<strong> print(s)<\/strong><\/p>\n<p><strong>o<\/strong><br \/>\n<strong> e<\/strong><br \/>\n<strong> l<\/strong><br \/>\n<strong> h<\/strong><\/p>\n<p>Since sets are mutable, there are some things we cannot do with them: for example, we cannot use them as dictionary keys. But objects of type frozenset are immutable. Therefore:<\/p>\n<p><strong>&gt;&gt;&gt; unchangeableSet = frozenset(&#8216;abc&#8217;)<\/strong><\/p>\n<p>Although we can&#8217;t add and remove items, we can perform the usual set operations on frozensets, or a combination of sets and frozensets:<\/p>\n<p><strong>&gt;&gt;&gt; mySet | unchangeableSet<\/strong><br \/>\n<strong> {&#8216;o&#8217;, &#8216;e&#8217;, &#8216;b&#8217;, &#8216;h&#8217;, &#8216;c&#8217;, &#8216;a&#8217;, &#8216;l&#8217;}<\/strong><\/p>\n<p>For an example of using frozenset to create keys for a dictionary, here&#8217;s a sample:<\/p>\n<p><strong>&gt;&gt;&gt; keySet = frozenset(&#8216;abc&#8217;)<\/strong><br \/>\n<strong> &gt;&gt;&gt; names = [ &#8216;Able&#8217;, &#8216;Baker&#8217;, &#8216;Charlie&#8217; ]<\/strong><br \/>\n<strong> &gt;&gt;&gt; myDict = { }<\/strong><br \/>\n<strong> &gt;&gt;&gt; i = 0<\/strong><br \/>\n<strong> &gt;&gt;&gt; for s in keySet:<\/strong><br \/>\n<strong> myDict[s] = names[i]<\/strong><br \/>\n<strong> i += 1<\/strong><\/p>\n<p><strong>&gt;&gt;&gt; print(myDict)<\/strong><br \/>\n<strong> {&#8216;a&#8217;: &#8216;Able&#8217;, &#8216;b&#8217;: &#8216;Baker&#8217;, &#8216;c&#8217;: &#8216;Charlie&#8217;}<\/strong><\/p>\n<p>Here, we created an immutable set called keySet, and a list of items to put in our dictionary. We iterate through the set, mapping items in the list to keys in keySet. When we print out the results, we see that each item was successfully mapped to a key.<\/p>\n<p>In the next article, we will continue our look at sets.<\/p>\n<h3><span style=\"text-decoration: underline;\"><strong>External Links:<\/strong><\/span><\/h3>\n<p><a href=\"https:\/\/docs.python.org\/3.4\/library\/stdtypes.html?highlight=set\">Set, frozenset at docs.python.org<\/a> &#8211; Official documentation on sets and frozensets for Python 3.4<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the core Python data types that we did not mention in earlier articles that perhaps deserves some attention is the set. Sets are a recent addition to Python; they are neither mappings (dictionaries) nor sequences (strings, lists and typles). Sets are created by calling the built-in set function or using new set literals [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,3],"tags":[23,57,58,4,56],"class_list":["post-87","post","type-post","status-publish","format-standard","hentry","category-guides","category-tutorials","tag-dictionary","tag-frozensets","tag-mutable","tag-python","tag-sets","entry"],"_links":{"self":[{"href":"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-json\/wp\/v2\/posts\/87","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-json\/wp\/v2\/comments?post=87"}],"version-history":[{"count":2,"href":"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-json\/wp\/v2\/posts\/87\/revisions"}],"predecessor-version":[{"id":90,"href":"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-json\/wp\/v2\/posts\/87\/revisions\/90"}],"wp:attachment":[{"href":"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-json\/wp\/v2\/media?parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-json\/wp\/v2\/categories?post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-json\/wp\/v2\/tags?post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}