Latest recipes tagged "sort"http://code.activestate.com/recipes/tags/sort/new/2017-02-11T19:01:52-08:00ActiveState Code RecipesOrdered Listbox for Tkinter (Python) 2017-02-11T19:01:52-08:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580756-ordered-listbox-for-tkinter/ <p style="color: grey"> Python recipe 580756 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/listbox/">listbox</a>, <a href="/recipes/tags/ordered/">ordered</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/sorted/">sorted</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 6. </p> <p>Listbox megawidget showing ordered strings. It's possible to ignore case. I also provided an auto scrollbar option.</p> Dependency resolution (Python) 2016-04-14T12:58:40-07:00Mike 'Fuzzy' Partinhttp://code.activestate.com/recipes/users/4179778/http://code.activestate.com/recipes/580642-dependency-resolution/ <p style="color: grey"> Python recipe 580642 by <a href="/recipes/users/4179778/">Mike 'Fuzzy' Partin</a> (<a href="/recipes/tags/compare/">compare</a>, <a href="/recipes/tags/dependency/">dependency</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/sorting/">sorting</a>). </p> <p>This recipe shows how to take a list of objects, each with their own list of dependencies, and resolve them to proper order. It includes some poor mans circular dependency detection (very poor mans).</p> topological sorting again (Python) 2013-03-06T19:21:11-08:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578406-topological-sorting-again/ <p style="color: grey"> Python recipe 578406 by <a href="/recipes/users/4184815/">yota</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/topological/">topological</a>). Revision 9. </p> <p>Topological sorting is the answer to the following question : in a direct acyclic graph, how can I pick up nodes "in order", such as upstream nodes are always before downstream ones ? Many solutions may exists, many algorithms too.</p> <p>Alas, it seems I'm too stupid to understand already proposed recipes on topological sorting. Hopefully I do grasp the "write once, read many" concept.</p> <p>Here, you will find a plain algorithm, optimized only for code clarity, of a topological sorting for direct acyclic graphs, implemented in python from the pseudo code found on <a href="http://en.wikipedia.org/wiki/Topological_sorting">wikipedia</a>:</p> <pre class="prettyprint"><code>L ← Empty list that will contain the sorted elements S ← Set of all nodes with no incoming edges while S is non-empty do remove a node n from S insert n into L for each node m with an edge e from n to m do remove edge e from the graph if m has no other incoming edges then insert m into S if graph has edges then return error (graph has at least one cycle) else return L (a topologically sorted order) </code></pre> <p>Only tested with python3.2, should work with other versions. Be careful, code indented with tabs, since space is evil è_é</p> Topological Sort (Python) 2012-09-27T12:21:23-07:00Sam Dentonhttp://code.activestate.com/recipes/users/4172262/http://code.activestate.com/recipes/578272-topological-sort/ <p style="color: grey"> Python recipe 578272 by <a href="/recipes/users/4172262/">Sam Denton</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/sorting/">sorting</a>, <a href="/recipes/tags/topological/">topological</a>, <a href="/recipes/tags/toposort/">toposort</a>). </p> <p>A topological sort (sometimes abbreviated topsort or toposort) or topological ordering of a directed graph is a linear ordering of its vertices such that, for every edge uv, u comes before v in the ordering. For instance, the vertices of the graph may represent tasks to be performed, and the edges may represent constraints that one task must be performed before another; in this application, a topological ordering is just a valid sequence for the tasks. A topological ordering is possible if and only if the graph has no directed cycles, that is, if it is a directed acyclic graph (DAG).</p> Python Binary Search Tree (Python) 2011-08-08T00:15:09-07:00sivarama sarmahttp://code.activestate.com/recipes/users/4178890/http://code.activestate.com/recipes/577830-python-binary-search-tree/ <p style="color: grey"> Python recipe 577830 by <a href="/recipes/users/4178890/">sivarama sarma</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/maximum/">maximum</a>, <a href="/recipes/tags/minimum/">minimum</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/sort/">sort</a>). </p> <p>A data structure that holds a sorted collection of values, and supports efficient insertion, deletion, sorted iteration, and min/max finding. Values may sorted either based on their natural ordering, or on a key function (specified as an argument to the search tree's constructor). The search tree may contain duplicate values (or multiple values with equal keys) -- the ordering of such values is undefined.</p> <p>This implementation was made with efficiency in mind. In particular, it is more than twice as fast as the other native-Python implementations I tried (which all use objects to store search tree nodes).</p> <p>See also: <a href="http://en.wikipedia.org/wiki/Binary_search_tree">http://en.wikipedia.org/wiki/Binary_search_tree</a>, <a href="http://en.wikipedia.org/wiki/A*_search_algorithm">http://en.wikipedia.org/wiki/A*_search_algorithm</a></p> BitSort (Python) 2011-06-19T14:03:56-07:00wong2http://code.activestate.com/recipes/users/4178345/http://code.activestate.com/recipes/577763-bitsort/ <p style="color: grey"> Python recipe 577763 by <a href="/recipes/users/4178345/">wong2</a> (<a href="/recipes/tags/sort/">sort</a>). </p> <p>BitSort in python. using bitarray library</p> SleepSort (Timer version) (Python) 2011-06-18T05:37:06-07:00wong2http://code.activestate.com/recipes/users/4178345/http://code.activestate.com/recipes/577762-sleepsort-timer-version/ <p style="color: grey"> Python recipe 577762 by <a href="/recipes/users/4178345/">wong2</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/interest/">interest</a>, <a href="/recipes/tags/sort/">sort</a>). </p> <p>Python version sleep sort by using threading.Timer</p> heap sort (Python) 2011-05-12T01:39:18-07:00huang chongdihttp://code.activestate.com/recipes/users/4177954/http://code.activestate.com/recipes/577688-heap-sort/ <p style="color: grey"> Python recipe 577688 by <a href="/recipes/users/4177954/">huang chongdi</a> (<a href="/recipes/tags/heap/">heap</a>, <a href="/recipes/tags/sort/">sort</a>). </p> <p>a python version of heap sort</p> naive natural sort (Python) 2011-08-13T16:47:36-07:00Romain Dartigueshttp://code.activestate.com/recipes/users/4167472/http://code.activestate.com/recipes/577679-naive-natural-sort/ <p style="color: grey"> Python recipe 577679 by <a href="/recipes/users/4167472/">Romain Dartigues</a> (<a href="/recipes/tags/ascii/">ascii</a>, <a href="/recipes/tags/sort/">sort</a>). </p> <p>I wrote this after reading The Alphanum Algorithm (<a href="http://www.davekoelle.com/alphanum.html" rel="nofollow">http://www.davekoelle.com/alphanum.html</a>) by David Koelle a few years ago; my goal was to improve the performances of the Python version of his scripts.</p> <p>My version is approximatly 10 times faster than it's <code>alphanum.py</code> and about 3 times faster than the <code>alphanum.py_v2.4</code> on my computer, yielding the same results (for non-unicode at least).</p> <p><strong>Note</strong>: see the version of wizkid in the comments which is even faster.</p> Python Binary Search Tree (Python) 2011-01-10T02:27:08-08:00Edward Loperhttp://code.activestate.com/recipes/users/2637812/http://code.activestate.com/recipes/577540-python-binary-search-tree/ <p style="color: grey"> Python recipe 577540 by <a href="/recipes/users/2637812/">Edward Loper</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/maximum/">maximum</a>, <a href="/recipes/tags/minimum/">minimum</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/sort/">sort</a>). Revision 2. </p> <p>A data structure that holds a sorted collection of values, and supports efficient insertion, deletion, sorted iteration, and min/max finding. Values may sorted either based on their natural ordering, or on a key function (specified as an argument to the search tree's constructor). The search tree may contain duplicate values (or multiple values with equal keys) -- the ordering of such values is undefined.</p> <p>This implementation was made with efficiency in mind. In particular, it is more than twice as fast as the other native-Python implementations I tried (which all use objects to store search tree nodes).</p> <p>See also: <a href="http://en.wikipedia.org/wiki/Binary_search_tree">http://en.wikipedia.org/wiki/Binary_search_tree</a>, <a href="http://en.wikipedia.org/wiki/A*_search_algorithm">http://en.wikipedia.org/wiki/A*_search_algorithm</a></p> Sort file (with unique option) (JavaScript) 2010-05-30T21:47:36-07:00Glenn Jenkinshttp://code.activestate.com/recipes/users/4174057/http://code.activestate.com/recipes/577247-sort-file-with-unique-option/ <p style="color: grey"> JavaScript recipe 577247 by <a href="/recipes/users/4174057/">Glenn Jenkins</a> (<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/komodo/">komodo</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/unique/">unique</a>). </p> <p>This will sort the lines in the current document into alphabetical order. It doesn't apply any special rule (so 5 will come <em>after</em> 42).</p> <p>On running, it asks if you want a unique sort (Cancel for no, Ok for yes), in which case it'll remove duplicate lines.</p> Merge multiple (potentially infinite) sorted inputs into a single sorted output (Python) 2010-04-01T04:54:16-07:00Gabriel Genellinahttp://code.activestate.com/recipes/users/924636/http://code.activestate.com/recipes/577041-merge-multiple-potentially-infinite-sorted-inputs-/ <p style="color: grey"> Python recipe 577041 by <a href="/recipes/users/924636/">Gabriel Genellina</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/merge/">merge</a>, <a href="/recipes/tags/sort/">sort</a>). Revision 4. </p> <p>Merge a (possibly infinite) number of already sorted inputs (each of possibly infinite length) into a single sorted output.</p> <p>Similar to heapq.merge and sorted(itertools.chain(*iterables)).</p> <p>Like heapq.merge, returns a generator, does not pull the data into memory all at once, and assumes that each of the input iterables is already sorted (smallest to largest).</p> <p>Unlike heapq.merge, accepts an infinite number of input iterables, but requires all of them to come in ascending order (that is, their starting point must come in ascending order).</p> <p>In addition, accepts a <em>key</em> function (like <code>sorted</code>, <code>min</code>, <code>max</code>, etc.)</p> Functional selection sort (Python) 2009-09-29T13:34:35-07:00pavelhttp://code.activestate.com/recipes/users/4171837/http://code.activestate.com/recipes/576917-functional-selection-sort/ <p style="color: grey"> Python recipe 576917 by <a href="/recipes/users/4171837/">pavel</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/reduce/">reduce</a>, <a href="/recipes/tags/selection/">selection</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/sorting/">sorting</a>). Revision 3. </p> <p>This is a variant of selection sort without using for-statements. Do you like it? :-)</p> Sorting big files the Python 2.6 way (Python) 2009-05-30T21:51:09-07:00Gabriel Genellinahttp://code.activestate.com/recipes/users/924636/http://code.activestate.com/recipes/576755-sorting-big-files-the-python-26-way/ <p style="color: grey"> Python recipe 576755 by <a href="/recipes/users/924636/">Gabriel Genellina</a> (<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/sorting/">sorting</a>, <a href="/recipes/tags/text/">text</a>). Revision 3. </p> <p>This is just a rewrite of <a href="http://code.activestate.com/recipes/466302/">Recipe 466302</a> "Sorting big files the Python 2.4 way", taking advantage of heapq.merge, context managers, and other niceties of newer Python versions. It can be used to sort very large files (millions of records) in Python. No record termination character is required, hence a record may contain embedded binary data, newlines, etc. You can specify how many temporary files to use and where they are located.</p> Sort sections and keys in .ini file (Python) 2008-12-19T05:46:05-08:00Michal Niklashttp://code.activestate.com/recipes/users/186902/http://code.activestate.com/recipes/576587-sort-sections-and-keys-in-ini-file/ <p style="color: grey"> Python recipe 576587 by <a href="/recipes/users/186902/">Michal Niklas</a> (<a href="/recipes/tags/config/">config</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/ini/">ini</a>, <a href="/recipes/tags/sort/">sort</a>). Revision 3. </p> <p>I use this program when I want to make .ini file more readable or compare two .ini files</p> Sort strings containing german umlauts in correct order (Python) 2008-09-13T14:28:54-07:00Andreas Maierhttp://code.activestate.com/recipes/users/4167085/http://code.activestate.com/recipes/576507-sort-strings-containing-german-umlauts-in-correct-/ <p style="color: grey"> Python recipe 576507 by <a href="/recipes/users/4167085/">Andreas Maier</a> (<a href="/recipes/tags/din5007/">din5007</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/umlaut/">umlaut</a>). Revision 13. </p> <p>A function which implements sort keys for the german language according to DIN 5007.</p> Arbitrary order attribute writing with ElementTree (Python) 2008-08-01T19:24:34-07:00Orri Ganelhttp://code.activestate.com/recipes/users/2259404/http://code.activestate.com/recipes/576403-arbitrary-order-attribute-writing-with-elementtree/ <p style="color: grey"> Python recipe 576403 by <a href="/recipes/users/2259404/">Orri Ganel</a> (<a href="/recipes/tags/attribute/">attribute</a>, <a href="/recipes/tags/element/">element</a>, <a href="/recipes/tags/elementtree/">elementtree</a>, <a href="/recipes/tags/element_tree/">element_tree</a>, <a href="/recipes/tags/etree/">etree</a>, <a href="/recipes/tags/order/">order</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/write/">write</a>, <a href="/recipes/tags/writing/">writing</a>, <a href="/recipes/tags/xml/">xml</a>). Revision 5. </p> <p>Modified version of ElementTree with two additional parameters to the write() method: "sortflag" and "sortcmp". "sortflag" defaults to "default", which results in unmodified behavior. "sortcmp" defaults to None, which results in unmodified behavior. See discussion for usage and justification. Changes made begin on line 655.</p> <p>EDIT: in most cases, unless sortflag happened to be intended for the root, it would be ignored; added sortflag and sortcmp to self._write() call on line 724. Expect another revision in the near future to allow for specifying different orders for different xml tags.</p> <p>EDIT, the second: Added tag-specific ordering.</p>