Popular Python recipes tagged "set"http://code.activestate.com/recipes/langs/python/tags/set/2016-04-05T18:11:49-07:00ActiveState Code RecipesMerge unique items from multiple lists into a new list (Python) 2016-04-05T18:11:49-07:00Johannes Shttp://code.activestate.com/recipes/users/4193888/http://code.activestate.com/recipes/580634-merge-unique-items-from-multiple-lists-into-a-new-/ <p style="color: grey"> Python recipe 580634 by <a href="/recipes/users/4193888/">Johannes S</a> (<a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/merge/">merge</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/set/">set</a>). Revision 2. </p> <p>Suppose you have multiple lists. You want to print all unique items from the list. Now, what you could do is merge the lists into one_big_list (e.g., a + b +c), and then iterate over each item in one_big_list, etc. The solution proposed here gets this done faster and in one line of code. How? By using <strong>a python set</strong>. A python set is a dictionary that contains only keys (and no values). And dictionary keys are, by definition, unique. Hence, duplicate items are weeded out automatically. Once you have the set, you can easily convert it back into a list. As easy as that!</p> Delaunay triangulation (Python) 2015-02-08T15:24:11-08:00Alexander Pletzerhttp://code.activestate.com/recipes/users/4190754/http://code.activestate.com/recipes/579021-delaunay-triangulation/ <p style="color: grey"> Python recipe 579021 by <a href="/recipes/users/4190754/">Alexander Pletzer</a> (<a href="/recipes/tags/delaunay/">delaunay</a>, <a href="/recipes/tags/edge/">edge</a>, <a href="/recipes/tags/point/">point</a>, <a href="/recipes/tags/set/">set</a>, <a href="/recipes/tags/tessellation/">tessellation</a>, <a href="/recipes/tags/triangles/">triangles</a>, <a href="/recipes/tags/triangulation/">triangulation</a>, <a href="/recipes/tags/vertices/">vertices</a>). </p> <p>Given a set of points, this code will construct a Delaunay triangulation.</p> OrderedSet (Python) 2015-06-25T17:33:13-07:00Sanderhttp://code.activestate.com/recipes/users/4192426/http://code.activestate.com/recipes/579071-orderedset/ <p style="color: grey"> Python recipe 579071 by <a href="/recipes/users/4192426/">Sander</a> (<a href="/recipes/tags/ordered/">ordered</a>, <a href="/recipes/tags/set/">set</a>). </p> <p>Set that remembers original insertion order.</p> OrderedSet (Python) 2012-12-19T07:12:32-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576694-orderedset/ <p style="color: grey"> Python recipe 576694 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/ordered/">ordered</a>, <a href="/recipes/tags/set/">set</a>). Revision 9. </p> <p>Set that remembers original insertion order.</p> List comparison, difference and more using set & frozenset (Python) 2012-11-01T10:30:58-07:00Scott S-Allenhttp://code.activestate.com/recipes/users/4181178/http://code.activestate.com/recipes/578310-list-comparison-difference-and-more-using-set-froz/ <p style="color: grey"> Python recipe 578310 by <a href="/recipes/users/4181178/">Scott S-Allen</a> (<a href="/recipes/tags/compare/">compare</a>, <a href="/recipes/tags/contain/">contain</a>, <a href="/recipes/tags/difference/">difference</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/remove/">remove</a>, <a href="/recipes/tags/set/">set</a>, <a href="/recipes/tags/union/">union</a>). </p> <p>Python has a powerful suite of tools for comparing lists by way of sets and frozensets. Here are a few examples and conveniences that many newcomers, even a few seasoned developers, are unaware.</p> Representing a set in a compact way (Python) 2011-04-24T10:55:45-07:00jimmy2timeshttp://code.activestate.com/recipes/users/4177690/http://code.activestate.com/recipes/577660-representing-a-set-in-a-compact-way/ <p style="color: grey"> Python recipe 577660 by <a href="/recipes/users/4177690/">jimmy2times</a> (<a href="/recipes/tags/compact/">compact</a>, <a href="/recipes/tags/compressed/">compressed</a>, <a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/set/">set</a>, <a href="/recipes/tags/structure/">structure</a>). Revision 5. </p> <p>Implements a data structure for sets of natural numbers, represented by a single integer. The interface is similar to the built-in structure 'set'.</p> setdict (Python) 2010-11-27T03:00:28-08:00thom nealehttp://code.activestate.com/recipes/users/4176069/http://code.activestate.com/recipes/577471-setdict/ <p style="color: grey"> Python recipe 577471 by <a href="/recipes/users/4176069/">thom neale</a> (<a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/set/">set</a>). </p> <p>Set-like operations for dictionaries</p> Union-Find (Python) 2010-05-10T20:32:15-07:00Ahmed El Deebhttp://code.activestate.com/recipes/users/4173897/http://code.activestate.com/recipes/577225-union-find/ <p style="color: grey"> Python recipe 577225 by <a href="/recipes/users/4173897/">Ahmed El Deeb</a> (<a href="/recipes/tags/disjoint/">disjoint</a>, <a href="/recipes/tags/forests/">forests</a>, <a href="/recipes/tags/set/">set</a>, <a href="/recipes/tags/unionfind/">unionfind</a>). </p> <p>A.k.a. Disjoint set forests. Minimalistic implementation. Directly ported from pseudo code on the Wikipedia page: <a href="http://en.wikipedia.org/wiki/Disjoint-set_data_structure">http://en.wikipedia.org/wiki/Disjoint-set_data_structure</a></p> <p>Employs path compression and union by rank as described in the link above.</p> Sets with a custom equality/uniqueness function (Python) 2009-10-29T21:08:22-07:00Gabriel Genellinahttp://code.activestate.com/recipes/users/924636/http://code.activestate.com/recipes/576932-sets-with-a-custom-equalityuniqueness-function/ <p style="color: grey"> Python recipe 576932 by <a href="/recipes/users/924636/">Gabriel Genellina</a> (<a href="/recipes/tags/abstract_base_class/">abstract_base_class</a>, <a href="/recipes/tags/mutableset/">mutableset</a>, <a href="/recipes/tags/set/">set</a>, <a href="/recipes/tags/unique/">unique</a>). Revision 4. </p> <p>The builtin <code>set</code> and <code>frozenset</code> types are based on object equality; they call __eq__ to determine whether an object is a member of the set or not. But there are cases when one needs a set of objects that are compared by other means, apart from the default __eq__ function. There are several ways to achieve that; this recipe presents two classes, FrozenKeyedSet and KeyedSet, that take an additional function <code>key</code> which is used to determine membership and uniqueness. Given two objects which return the same value for <code>key</code>, only one of them will be in the set.</p> Set product (Python) 2010-04-12T19:06:22-07:00Paul Ghttp://code.activestate.com/recipes/users/4173653/http://code.activestate.com/recipes/577190-set-product/ <p style="color: grey"> Python recipe 577190 by <a href="/recipes/users/4173653/">Paul G</a> (<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/multiply/">multiply</a>, <a href="/recipes/tags/product/">product</a>, <a href="/recipes/tags/set/">set</a>). </p> <p>This recipe provides a subclass of set which implements __mul__ and __pow__ to support the computation of set products. When multiplying two sets together, a new set is produced with elements which are the inter-set pairs.</p>