Top-rated recipes tagged "search"http://code.activestate.com/recipes/tags/search/top/2017-04-08T12:27:36-07:00ActiveState Code RecipesPython 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> "To sort a dictionary" (Python) 2001-04-08T19:35:01-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/52306-to-sort-a-dictionary/ <p style="color: grey"> Python recipe 52306 by <a href="/recipes/users/97991/">Alex Martelli</a> (<a href="/recipes/tags/search/">search</a>). Revision 2. </p> <p>Dictionaries can't be sorted -- a mapping has no ordering! -- so, when you feel the need to sort one, you no doubt want to sort its <em>keys</em> (in a separate list). Sorting (key,value) pairs (items) is simplest, but not fastest.</p> Sorting dictionaries by value in Python 2.4 (Python) 2004-09-13T04:19:24-07:00Nick Coghlanhttp://code.activestate.com/recipes/users/2035254/http://code.activestate.com/recipes/304440-sorting-dictionaries-by-value-in-python-24/ <p style="color: grey"> Python recipe 304440 by <a href="/recipes/users/2035254/">Nick Coghlan</a> (<a href="/recipes/tags/search/">search</a>). </p> <p>Python 2.4 adds a new builtin function sorted(), which can make obtaining the items of a dictionary sorted by key or value a single line operation.</p> Ask a question with Yahoo! Answers and YQL (Python) 2010-11-02T12:55:01-07:00Anand B Pillaihttp://code.activestate.com/recipes/users/4169530/http://code.activestate.com/recipes/577449-ask-a-question-with-yahoo-answers-and-yql/ <p style="color: grey"> Python recipe 577449 by <a href="/recipes/users/4169530/">Anand B Pillai</a> (<a href="/recipes/tags/answers/">answers</a>, <a href="/recipes/tags/query/">query</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/yahoo/">yahoo</a>, <a href="/recipes/tags/yql/">yql</a>). </p> <p>The web has a lot of resources where on can get answers to questions such as wiki Answers and Yahoo! answers. Of this Y! answers provide a quick and easy way to query it by using Yahoo's own query language, YQL. This recipe demonstrates using Yahoo! answers, YQL and Python to get an answer to your query on the command line.</p> Reorder a sequence (uses generators, and recursion!) (Python) 2003-11-28T12:54:43-08:00Michael Davieshttp://code.activestate.com/recipes/users/1502767/http://code.activestate.com/recipes/252178-reorder-a-sequence-uses-generators-and-recursion/ <p style="color: grey"> Python recipe 252178 by <a href="/recipes/users/1502767/">Michael Davies</a> (<a href="/recipes/tags/search/">search</a>). </p> <p>Small function to generate every permutation of a given sequence. Works for lists and strings</p> Robot Pager (Search engines and others) (Python) 2010-10-10T19:33:20-07:00Carlos del Ojohttp://code.activestate.com/recipes/users/4173977/http://code.activestate.com/recipes/577420-robot-pager-search-engines-and-others/ <p style="color: grey"> Python recipe 577420 by <a href="/recipes/users/4173977/">Carlos del Ojo</a> (<a href="/recipes/tags/automate/">automate</a>, <a href="/recipes/tags/engine/">engine</a>, <a href="/recipes/tags/paging/">paging</a>, <a href="/recipes/tags/robot/">robot</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/websites/">websites</a>). Revision 3. </p> <p>This is a class to make easy the development of robots, to parse results over a website with a paging. For example Google, Yahoo, Bing, or any other page with paging system.</p> <p>PagerEngine is the main class. I've developed three more clases implementing GoogleSearch, YahooSearch and BingSearch as examples.</p> <p>Inheriting from PagerEngine (and having RexExp knowledge) you can easily develop other robots for other websites.</p> get_equivalent(container, item) (Python) 2009-04-29T10:33:24-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/499299-get_equivalentcontainer-item/ <p style="color: grey"> Python recipe 499299 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/search/">search</a>). Revision 3. </p> <p>Extracts the specific element matched by "item in container". Useful for interning or caching applications needing canonical or singleton values.</p> Lazy sorting (Python) 2004-05-03T12:30:10-07:00Matteo Dell'Amicohttp://code.activestate.com/recipes/users/1782068/http://code.activestate.com/recipes/280501-lazy-sorting/ <p style="color: grey"> Python recipe 280501 by <a href="/recipes/users/1782068/">Matteo Dell'Amico</a> (<a href="/recipes/tags/search/">search</a>). Revision 2. </p> <p>This generator will sort only the requested part of a list. Useful for getting just the first few elements of a list and avoiding the overhead of sorting the whole thing.</p> Select the nth smallest element (Python) 2004-03-05T08:51:37-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/269554-select-the-nth-smallest-element/ <p style="color: grey"> Python recipe 269554 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/search/">search</a>). Revision 6. </p> <p>O(n) quicksort style algorithm for looking up data based on rank order. Useful for finding medians, percentiles, quartiles, and deciles. Equivalent to data[n] when the data is already sorted.</p> Finding the index of an item in embedded sequences (Python) 2001-06-19T22:15:09-07:00Brett Cannonhttp://code.activestate.com/recipes/users/98030/http://code.activestate.com/recipes/65252-finding-the-index-of-an-item-in-embedded-sequences/ <p style="color: grey"> Python recipe 65252 by <a href="/recipes/users/98030/">Brett Cannon</a> (<a href="/recipes/tags/search/">search</a>). </p> <p>This function will return a list containing the indices needed to reach an item in embedded sequences.</p> <p>So deepindex([[1,2],[3,[4,[5,6]]],7,[8,9]],6) will return [1,1,1,1].</p> Tkinter search box (Python) 2017-04-08T12:27:36-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580773-tkinter-search-box/ <p style="color: grey"> Python recipe 580773 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/entry/">entry</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/searchbox/">searchbox</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 9. </p> <p>Instead of using two colors for active background and normal background, I use only one color and opacity parameter.</p> <p>I trigger the feeling of a button using different colors when the mouse is and isn't over. Many modern HTML search boxes uses the same approach.</p> <p>Command function receives text of entry box when button is pressed.</p> grep in Python (Python) 2014-03-05T19:47:50-08:00Andy Dustmanhttp://code.activestate.com/recipes/users/2435929/http://code.activestate.com/recipes/578845-grep-in-python/ <p style="color: grey"> Python recipe 578845 by <a href="/recipes/users/2435929/">Andy Dustman</a> (<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/grep/">grep</a>, <a href="/recipes/tags/search/">search</a>). </p> <p>The grep() function is inspired by UNIX grep, but is not limited to string patterns and files or streams.</p> Simple graph algorithms with a modular design (Python) 2011-04-21T13:40:32-07:00jimmy2timeshttp://code.activestate.com/recipes/users/4177690/http://code.activestate.com/recipes/577668-simple-graph-algorithms-with-a-modular-design/ <p style="color: grey"> Python recipe 577668 by <a href="/recipes/users/4177690/">jimmy2times</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/breadth/">breadth</a>, <a href="/recipes/tags/depth/">depth</a>, <a href="/recipes/tags/directed/">directed</a>, <a href="/recipes/tags/first/">first</a>, <a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/object/">object</a>, <a href="/recipes/tags/oriented/">oriented</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/theory/">theory</a>, <a href="/recipes/tags/undirected/">undirected</a>, <a href="/recipes/tags/visit/">visit</a>). Revision 7. </p> <p>The purpose of this recipe is to look at algorithmic graph theory from an object-oriented perspective.</p> <p>A graph is built on top of a dictionary indexed by its vertices, each item being the set of neighbours of the key vertex. This ensures that iterating through the neighbours of a vertex is still efficient in sparse graphs (as with adjacency lists) while at the same time checking for adjacency is expected constant-time (as with the adjacency matrix).</p> <p>Any valid class of graph must implement the interface defined by AbstractGraph.</p> <p>A generic search algorithm takes as input a graph, source and target vertices and a queue. A queue must implement the methods Q.get(), Q.put() and Q.empty() in such a way to get the desired order in visiting the vertices.</p> <p>Given this pattern, breadth-first and depth-first search are essentially defined by the corresponding expansion policies: the first one uses an actual FIFO queue, the second one a LIFO queue (or stack).</p> kd-tree for nearest neighbor search in a k-dimensional space (Python) 2010-12-17T15:49:08-08:00Matteo Dell'Amicohttp://code.activestate.com/recipes/users/2433284/http://code.activestate.com/recipes/577497-kd-tree-for-nearest-neighbor-search-in-a-k-dimensi/ <p style="color: grey"> Python recipe 577497 by <a href="/recipes/users/2433284/">Matteo Dell'Amico</a> (<a href="/recipes/tags/nearest/">nearest</a>, <a href="/recipes/tags/neighbor/">neighbor</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/tree/">tree</a>). Revision 5. </p> <p>The kd-tree can be used to organize efficient search for nearest neighbors in a k-dimensional space.</p> Identifying network name (CIDR) for IPv4Address (Python) 2011-12-20T21:53:30-08:00DJChttp://code.activestate.com/recipes/users/4166733/http://code.activestate.com/recipes/576479-identifying-network-name-cidr-for-ipv4address/ <p style="color: grey"> Python recipe 576479 by <a href="/recipes/users/4166733/">DJC</a> (<a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/trie/">trie</a>). Revision 3. </p> <p>This program uses a digital trie search (Knuth 1998) to find the network of a given IPv4 address. It is fast, and can find nested subnets. </p> Groupby hierarchy tree (Python) 2007-11-20T09:34:09-08:00Robert Lujohttp://code.activestate.com/recipes/users/4044016/http://code.activestate.com/recipes/535129-groupby-hierarchy-tree/ <p style="color: grey"> Python recipe 535129 by <a href="/recipes/users/4044016/">Robert Lujo</a> (<a href="/recipes/tags/search/">search</a>). </p> <p>Here is some thing i needed for some kind drill down (olap). It produces tree of grouped items in hierarchy.</p> recursive sorting (Python) 2006-01-20T06:49:10-08:00Nicola Munerattohttp://code.activestate.com/recipes/users/2745224/http://code.activestate.com/recipes/466321-recursive-sorting/ <p style="color: grey"> Python recipe 466321 by <a href="/recipes/users/2745224/">Nicola Muneratto</a> (<a href="/recipes/tags/search/">search</a>). </p> <p>Sort recursively nested lists</p> Pattern List (Python) 2005-10-31T04:54:30-08:00mark williamsonhttp://code.activestate.com/recipes/users/1219787/http://code.activestate.com/recipes/442497-pattern-list/ <p style="color: grey"> Python recipe 442497 by <a href="/recipes/users/1219787/">mark williamson</a> (<a href="/recipes/tags/search/">search</a>). </p> <p>I found my self want to express -string- in -list of regular expressions- and so I wrote this quick object to do the trick for me. It takes a list of strings containing regular expressions, compiles them into an internal list and then using the __contains__ operation ("in") looks to see if a given string contains any of the expressions. The first success returns True otherwise it returns False.</p> Sort a string using numeric order (Python) 2002-06-25T08:17:18-07:00Chui Teyhttp://code.activestate.com/recipes/users/98139/http://code.activestate.com/recipes/135435-sort-a-string-using-numeric-order/ <p style="color: grey"> Python recipe 135435 by <a href="/recipes/users/98139/">Chui Tey</a> (<a href="/recipes/tags/search/">search</a>). </p> <p>This recipe sorts a list of strings using the numeric order where possible.</p> Field Compare (Python) 2002-04-27T02:32:53-07:00Henk Punthttp://code.activestate.com/recipes/users/235719/http://code.activestate.com/recipes/123555-field-compare/ <p style="color: grey"> Python recipe 123555 by <a href="/recipes/users/235719/">Henk Punt</a> (<a href="/recipes/tags/search/">search</a>). </p> <p>A class that defines a compare function that can be used to sort lists of objects by any number of fields.</p>