Most viewed recipes tagged "merge"http://code.activestate.com/recipes/tags/merge/views/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>
Cheetah template processor for CSV and XML data (Python)
2010-05-21T10:00:14-07:00Raphaël Jolivethttp://code.activestate.com/recipes/users/4135673/http://code.activestate.com/recipes/577234-cheetah-template-processor-for-csv-and-xml-data/
<p style="color: grey">
Python
recipe 577234
by <a href="/recipes/users/4135673/">Raphaël Jolivet</a>
(<a href="/recipes/tags/cheetah/">cheetah</a>, <a href="/recipes/tags/csv/">csv</a>, <a href="/recipes/tags/merge/">merge</a>, <a href="/recipes/tags/populate/">populate</a>, <a href="/recipes/tags/template/">template</a>, <a href="/recipes/tags/xml/">xml</a>).
</p>
<p>This script allows to populate Cheetah text templates (<a href="http://www.cheetahtemplate.org/" rel="nofollow">http://www.cheetahtemplate.org/</a>) with XML or CSV input data.</p>
<p>This is useful in my day2day work, where I often need to quickly generate bunch of files based on a templates and data.</p>
<p>Cheetah template are very easy to write and understand, and I find it easy to use with CSV or XML data.</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>