Most viewed recipes tagged "search"http://code.activestate.com/recipes/tags/search/views/2017-04-08T12:27:36-07:00ActiveState Code Recipes"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>
Random selection of elements in a list, with no repeats (Python)
2002-06-05T05:50:10-07:00Iuri Wickerthttp://code.activestate.com/recipes/users/111694/http://code.activestate.com/recipes/59883-random-selection-of-elements-in-a-list-with-no-rep/
<p style="color: grey">
Python
recipe 59883
by <a href="/recipes/users/111694/">Iuri Wickert</a>
(<a href="/recipes/tags/search/">search</a>).
Revision 2.
</p>
<p>The simplest, direct way of consuming a list in a random fashion is painfully
slow for list with a few 100's of elements. There are equally simple, but much
faster ways to do this.</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>
Find Multiple Elements In a List (Python)
2011-11-05T23:40:41-07:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577943-find-multiple-elements-in-a-list/
<p style="color: grey">
Python
recipe 577943
by <a href="/recipes/users/4179768/">Alexander James Wallar</a>
(<a href="/recipes/tags/enumerate/">enumerate</a>, <a href="/recipes/tags/find/">find</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/search_list/">search_list</a>).
</p>
<p>This algorithm searches for more than one element in a list. The input is a list that you want to search through and a list of elements that you want to search for. The output is a multidimensional list with the positions of the elements you are searching for in the search list in the order that you listed them. </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>
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>
extract email addresses from files (Python)
2002-07-10T16:38:29-07:00carl scharenberghttp://code.activestate.com/recipes/users/184304/http://code.activestate.com/recipes/138889-extract-email-addresses-from-files/
<p style="color: grey">
Python
recipe 138889
by <a href="/recipes/users/184304/">carl scharenberg</a>
(<a href="/recipes/tags/search/">search</a>).
</p>
<p>A quick way to find valid email addresses in text files using a regular expression search. It then removes duplicate entries and returns the results in a list.</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>
Knuth-Morris-Pratt string matching (Python)
2002-03-01T23:49:23-08:00David Eppsteinhttp://code.activestate.com/recipes/users/218935/http://code.activestate.com/recipes/117214-knuth-morris-pratt-string-matching/
<p style="color: grey">
Python
recipe 117214
by <a href="/recipes/users/218935/">David Eppstein</a>
(<a href="/recipes/tags/search/">search</a>).
</p>
<p>This is an implementation of the Knuth-Morris-Pratt algorithm for finding copies of a given pattern as a contiguous subsequence of a larger text. Since KMP accesses the text only sequentially, it is natural to implement it in a way that allows the text to be an arbitrary iterator. After a preprocessing stage which takes time linear in the length of the pattern, each text symbol is processed in constant amortized time.</p>
Binary search and insert in Python (Python)
2001-04-25T05:09:37-07:00Noah Spurrierhttp://code.activestate.com/recipes/users/103276/http://code.activestate.com/recipes/54159-binary-search-and-insert-in-python/
<p style="color: grey">
Python
recipe 54159
by <a href="/recipes/users/103276/">Noah Spurrier</a>
(<a href="/recipes/tags/search/">search</a>).
Revision 3.
</p>
<p>This demonstrates a simple binary search through sorted data.
A binary search is a basic algorithm provided by bisect in Python.
The binary search can be summarized by two lines of code:
list.sort()
item_insert_point = bisect.bisect (list, item)</p>
Just For Fun: Quicksort in 3 Lines (Python)
2001-08-07T20:58:18-07:00Nathan Grayhttp://code.activestate.com/recipes/users/123436/http://code.activestate.com/recipes/66473-just-for-fun-quicksort-in-3-lines/
<p style="color: grey">
Python
recipe 66473
by <a href="/recipes/users/123436/">Nathan Gray</a>
(<a href="/recipes/tags/search/">search</a>).
Revision 4.
</p>
<p>OK, 4 lines if you count wrapped lines. :^) This is a rather naive implementation of quicksort that illustrates the expressive power of list comprehensions. DO NOT USE THIS IN REAL CODE!</p>
<p>NOTE: Due to an annoyance in the ASPN submission filters you must manually remove the space after the '\' character in the third line if you intend to use the code. Otherwise you will get a syntax error.</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>
Fuzzy matching dictionary (Python)
2006-03-18T11:23:25-08:00Mark Mc Mahonhttp://code.activestate.com/recipes/users/2819006/http://code.activestate.com/recipes/475148-fuzzy-matching-dictionary/
<p style="color: grey">
Python
recipe 475148
by <a href="/recipes/users/2819006/">Mark Mc Mahon</a>
(<a href="/recipes/tags/search/">search</a>).
</p>
<p>This is more an ease of use subclass of dict - rather then one that uses a lot of dict features.</p>
<p>If your requested item is in the dictionary (with a key that hashes the same) then it acts as a more or less normal dictionary.</p>
<p>If on the other hand you are looking for a string that is similar to a key in the dictionary, then the class iterates over all the keys and finds the one that is the closest match.</p>
Priority dict: a priority queue with updatable priorities (Python)
2007-06-26T09:20:06-07:00Matteo Dell'Amicohttp://code.activestate.com/recipes/users/4057132/http://code.activestate.com/recipes/522995-priority-dict-a-priority-queue-with-updatable-prio/
<p style="color: grey">
Python
recipe 522995
by <a href="/recipes/users/4057132/">Matteo Dell'Amico</a>
(<a href="/recipes/tags/search/">search</a>).
</p>
<p>You may have needed a priority queue with the ability to change the priorities of elements that were in the queue. This recipe solves this problem in an efficient way.</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>
guaranteed-stable sort with the decorate-sort-undecorate idiom (aka Schwartzian transform) (Python)
2001-06-04T18:47:59-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/52234-guaranteed-stable-sort-with-the-decorate-sort-unde/
<p style="color: grey">
Python
recipe 52234
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/search/">search</a>).
Revision 2.
</p>
<p>Python lists' .sort method is not guaranteed stable -- items that compare equal may or may not be in unchanged order. Ensuring stability is easy as one of the many application of the commom idiom decorate-sort-undecorate (aka "Schwartzian transform").</p>
Setting up a listbox filter in Tkinter(python 2.7) (Python)
2014-04-10T10:25:38-07:00Wyklephhttp://code.activestate.com/recipes/users/4189735/http://code.activestate.com/recipes/578860-setting-up-a-listbox-filter-in-tkinterpython-27/
<p style="color: grey">
Python
recipe 578860
by <a href="/recipes/users/4189735/">Wykleph</a>
(<a href="/recipes/tags/filter/">filter</a>, <a href="/recipes/tags/listbox/">listbox</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/searchbar/">searchbar</a>, <a href="/recipes/tags/tkinter/">tkinter</a>).
Revision 3.
</p>
<p>If you have a listbox and need to filter the contents of the listbox based on the contents of an entry field, this will help you! Shoutout to Nikola-K for the revision. It's much simpler now :D</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>
Python Multidimensional List Searcher (Python)
2011-10-29T22:21:39-07:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577929-python-multidimensional-list-searcher/
<p style="color: grey">
Python
recipe 577929
by <a href="/recipes/users/4179768/">Alexander James Wallar</a>
(<a href="/recipes/tags/element/">element</a>, <a href="/recipes/tags/elements/">elements</a>, <a href="/recipes/tags/find/">find</a>, <a href="/recipes/tags/finder/">finder</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/search/">search</a>).
</p>
<p>This module/function lets you find a 2 dimensional list of indices for elements you are looking for in a super list.
Example:</p>
<p>find([1,1,1,2,1,2,3,3],[1,2,3])</p>
<p>returns: [[0, 1, 2, 4], [3, 5], [6, 7]]</p>