Popular recipes tagged "lists"http://code.activestate.com/recipes/tags/lists/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>
Computing simple list functions recursively (Python)
2016-03-01T20:41:13-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580617-computing-simple-list-functions-recursively/
<p style="color: grey">
Python
recipe 580617
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/recursion/">recursion</a>, <a href="/recipes/tags/recursive/">recursive</a>).
</p>
<p>This recipe shows how to compute some simple list functions (functions that operate on lists) using simple recursive algorithms. Simple non-recursive algorithms exist for these tasks; this recipe is just for illustrative purposes. However, the principles described can be used for more complex recursive algorithms, for which non-recursive algorithms might be more complex than the recursive ones.</p>
Flattening an arbitrarily nested list in Python (Python)
2014-10-05T21:44:45-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578948-flattening-an-arbitrarily-nested-list-in-python/
<p style="color: grey">
Python
recipe 578948
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>This is a recipe to flatten a Python list which may have nested lists as items within it. It works for lists that have a maximum depth of nesting roughly equal to the recursion depth limit of Python, which is set to 1000 by default, though it can be increased with sys.setrecursionlimit().</p>
ChainedList and ChainedListView: Exposing Multiple Lists as a Single Sequence (Python)
2012-07-03T21:00:02-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/578190-chainedlist-and-chainedlistview-exposing-multiple-/
<p style="color: grey">
Python
recipe 578190
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/wrapper/">wrapper</a>).
</p>
<p>Handy for composing lists without losing the underlying indepedence. The "lists" don't actually have to be lists, though for ChainedList they must be mutable.</p>
Fast Sorting of n dimensional array by first dimension (Python)
2012-03-14T15:14:11-07:00Garretthttp://code.activestate.com/recipes/users/4181290/http://code.activestate.com/recipes/578074-fast-sorting-of-n-dimensional-array-by-first-dimen/
<p style="color: grey">
Python
recipe 578074
by <a href="/recipes/users/4181290/">Garrett</a>
(<a href="/recipes/tags/argsort/">argsort</a>, <a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sorting/">sorting</a>).
</p>
<p>I have looked far and wide for code for fast sorting of n dimensional arrays by the first element, for example if I had the array: <br />
ray = [[1,2,3,7,5][10,11,12,13,14]] </p>
<p>I would want it to come out as
ray = [[1,2,3,5,7][10,11,12,14,13]]</p>
<p>There are several ways to do this. One is
zipped = zip(<em>ray)
zipped.sort()
ray = zip(</em>zipped)</p>
<p>but this is extremely slow. Numpy has a much faster way to do it, but it wasn't immediately apparent.</p>
<p>if the above were a numpy array you could simply do the following:
indexes = numpy.argsort(ray[0])
for n in xrange(len(ray))
ray[n] = ray[n][indexes]</p>
<p>I did a time test of the two methods below.</p>
Base Expansion/Conversion Algorithm Python (Python)
2011-11-21T19:58:14-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577939-base-expansionconversion-algorithm-python/
<p style="color: grey">
Python
recipe 577939
by <a href="/recipes/users/4179768/">Alexander James Wallar</a>
(<a href="/recipes/tags/base/">base</a>, <a href="/recipes/tags/base16/">base16</a>, <a href="/recipes/tags/base2/">base2</a>, <a href="/recipes/tags/base_conversion/">base_conversion</a>, <a href="/recipes/tags/base_expansion/">base_expansion</a>, <a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/number_theory/">number_theory</a>, <a href="/recipes/tags/python/">python</a>).
Revision 3.
</p>
<p>This algorithm converts a base c number into a base b number. Parameters c and b are arbitrary are not constrained by any bounds. The input n is a list and the output is a list.</p>
Python Infinite Rotations and Tails (Python)
2011-02-13T15:27:42-08:00Thomas Ahlehttp://code.activestate.com/recipes/users/4060075/http://code.activestate.com/recipes/577574-python-infinite-rotations-and-tails/
<p style="color: grey">
Python
recipe 577574
by <a href="/recipes/users/4060075/">Thomas Ahle</a>
(<a href="/recipes/tags/cycles/">cycles</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/infinite/">infinite</a>, <a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/rotations/">rotations</a>, <a href="/recipes/tags/tails/">tails</a>).
</p>
<p>This is an example of how functional ideas of infinite lists can be used in python to optimize memory usage of certain problems.</p>
Fallout 3 "terminal hacking" minigame cracker (Python)
2008-12-20T11:18:35-08:00Bill Sharerhttp://code.activestate.com/recipes/users/4168512/http://code.activestate.com/recipes/576590-fallout-3-terminal-hacking-minigame-cracker/
<p style="color: grey">
Python
recipe 576590
by <a href="/recipes/users/4168512/">Bill Sharer</a>
(<a href="/recipes/tags/dictionaries/">dictionaries</a>, <a href="/recipes/tags/fallout_3_minigame/">fallout_3_minigame</a>, <a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/sets/">sets</a>, <a href="/recipes/tags/text/">text</a>).
</p>
<p>crackerhacker.py is a script I wrote as a Python learning exercise to help
solve Fallout 3's "terminal hacking" minigame. Many of you may already be
aware of this annoying mind challenge after playing the popular waste of
time from Bethesda Softworks on your favorite pc or console.</p>
<p>more details in the script</p>