Popular recipes tagged "map" but not "parallel"http://code.activestate.com/recipes/tags/map-parallel/2017-04-27T21:26:00-07:00ActiveState Code RecipesClassifying characters using nested conditional expressions (Python) 2017-04-27T21:26:00-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580792-classifying-characters-using-nested-conditional-ex/ <p style="color: grey"> Python recipe 580792 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/characters/">characters</a>, <a href="/recipes/tags/classification/">classification</a>, <a href="/recipes/tags/conditional_expressions/">conditional_expressions</a>, <a href="/recipes/tags/expressions/">expressions</a>, <a href="/recipes/tags/join/">join</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/map/">map</a>). </p> <p>Python has a feature called conditional expressions, similar to C's ternary operator. For example:</p> <p>print n, 'is odd' if n % 2 == 1 else 'is even'</p> <p>Here, the conditional expression is this part of the print statement above:</p> <p>'is odd' if n % 2 == 1 else 'is even'</p> <p>This expression evaluates to 'is odd' if the condition after the if keyword is True, and evaluates to 'is even' otherwise.</p> <p>The Python Language Reference section for conditional expressions shows that they can be nested. This recipe shows that we can use nested conditional expressions (within a return statement in a user-defined function) to classify characters into lowercase letters, uppercase letters, or neither.</p> <p>It also shows how to do the same task using map, lambda and string.join, again with a nested conditional expression, but without using return or a user-defined function.</p> Collection Pipeline in Python (Python) 2016-03-16T14:45:02-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/580625-collection-pipeline-in-python/ <p style="color: grey"> Python recipe 580625 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/filter/">filter</a>, <a href="/recipes/tags/map/">map</a>, <a href="/recipes/tags/pipe/">pipe</a>, <a href="/recipes/tags/pipline/">pipline</a>). </p> <p>A powerful functional programming technique is the use of pipelines of functions. If you have used shell scripting languages like <code>bash</code>, you will have used this technique. For instance, to count the number of files or directories, you might say: <code>ls | wc -l</code>. The output of the first command is piped into the input of the second, and the result returned.</p> <p>We can set up a similar pipeline using Lisp-like Map, Filter and Reduce special functions. Unlike the standard Python <code>map</code>, <code>filter</code> and <code>reduce</code>, these are designed to operate in a pipeline, using the same <code>|</code> syntax used by bash and other shell scripting languages:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; data = range(1000) &gt;&gt;&gt; data | Filter(lambda n: 20 &lt; n &lt; 30) | Map(float) | List [21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0] </code></pre> <p>The standard Python functional tools is much less attractive, as you have to write the functions in the opposite order to how they are applied to the data. This makes it harder to follow the logic of the expression.</p> <pre class="prettyprint"><code>&gt;&gt;&gt; list(map(float, filter(lambda n: 20 &lt; n &lt; 30, data))) [21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0] </code></pre> <p>We can also end the pipeline with a call to <code>Reduce</code> to collate the sequence into a single value. Here we take a string, extract all the digits, convert to ints, and multiply:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; from operator import mul &gt;&gt;&gt; "abcd12345xyz" | Filter(str.isdigit) | Map(int) | Reduce(mul) 120 </code></pre> An alternative way to draw parallels and meridians with basemap (Python) 2013-01-05T15:59:24-08:00PGhttp://code.activestate.com/recipes/users/4184598/http://code.activestate.com/recipes/578399-an-alternative-way-to-draw-parallels-and-meridians/ <p style="color: grey"> Python recipe 578399 by <a href="/recipes/users/4184598/">PG</a> (<a href="/recipes/tags/layout/">layout</a>, <a href="/recipes/tags/map/">map</a>, <a href="/recipes/tags/plots/">plots</a>, <a href="/recipes/tags/plotting/">plotting</a>). Revision 2. </p> <p>Basemap is a toolkit of matplotlib used to plot geographic maps.</p> <p>With this function you can:</p> <ul> <li><p>Draw the latitude/longitude grid easily in one line of code, specifying the lat/lon intervals.</p></li> <li><p>Use rcParams, so all your figures will look more consistent.</p></li> <li><p>Specify the label pad <em>in points</em> (instead of projection units).</p></li> <li><p>Place the labels indicating which margins will be used.</p></li> </ul> Plotting maps with Polar Stereographic projection focused in a region with Basemap (Python) 2012-12-15T11:49:52-08:00PGhttp://code.activestate.com/recipes/users/4184598/http://code.activestate.com/recipes/578379-plotting-maps-with-polar-stereographic-projection-/ <p style="color: grey"> Python recipe 578379 by <a href="/recipes/users/4184598/">PG</a> (<a href="/recipes/tags/map/">map</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/plotting/">plotting</a>, <a href="/recipes/tags/projections/">projections</a>, <a href="/recipes/tags/stereographic/">stereographic</a>). Revision 3. </p> <p>When plotting maps with either 'npstere' or 'spstere' projections with the Basemap toolkit for Matplotlib, the pole will always be placed in the center of the figure. With this function you can zoom the map to the area of interest by giving the latitude/longitudes coordinates of the bounds.</p> fmap(): a kind of inverse of the built-in Python map() function (Python) 2012-10-06T22:15:17-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578281-fmap-a-kind-of-inverse-of-the-built-in-python-map-/ <p style="color: grey"> Python recipe 578281 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/composition/">composition</a>, <a href="/recipes/tags/fmap/">fmap</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/map/">map</a>). </p> <p>The Python map() function returns a list of the results of applying the function to the items of the argument sequence(s).</p> <p>The fmap() function does the inverse, in a sense. It returns the result of applying a list of functions to a given argument.</p> Geocoding Lists via Google Maps (Python) 2012-05-11T05:06:27-07:00Mano Bastardohttp://code.activestate.com/recipes/users/4182040/http://code.activestate.com/recipes/578126-geocoding-lists-via-google-maps/ <p style="color: grey"> Python recipe 578126 by <a href="/recipes/users/4182040/">Mano Bastardo</a> (<a href="/recipes/tags/batch/">batch</a>, <a href="/recipes/tags/coordinates/">coordinates</a>, <a href="/recipes/tags/geocode/">geocode</a>, <a href="/recipes/tags/geocoding/">geocoding</a>, <a href="/recipes/tags/google/">google</a>, <a href="/recipes/tags/google_maps/">google_maps</a>, <a href="/recipes/tags/lat/">lat</a>, <a href="/recipes/tags/latitude/">latitude</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/list_comprehension/">list_comprehension</a>, <a href="/recipes/tags/lng/">lng</a>, <a href="/recipes/tags/longitude/">longitude</a>, <a href="/recipes/tags/map/">map</a>, <a href="/recipes/tags/web/">web</a>). Revision 2. </p> <p>A simple script written as an experiment in geocoding addresses in a database. A list of addresses in the form of "100 Any Street, Anytown, CA, 10010" is passed to a Google Maps URL, and the latitude/longitude coordinates are extracted from the returned XML.</p> <p>XML methods are not used in this script, but simple string searches instead.</p> Find function in a ram.map file (Python) 2011-12-23T21:58:01-08:00Sharonhttp://code.activestate.com/recipes/users/4180356/http://code.activestate.com/recipes/577995-find-function-in-a-rammap-file/ <p style="color: grey"> Python recipe 577995 by <a href="/recipes/users/4180356/">Sharon</a> (<a href="/recipes/tags/map/">map</a>, <a href="/recipes/tags/ram/">ram</a>, <a href="/recipes/tags/vxworks/">vxworks</a>). </p> <p>The findCloseAddress() function receives an input parameter ram.map file "ramMapFile", and search inside it for the function with the closest address to the input parameter "reqAddress".</p> map_longest and map_strict (Python) 2011-05-06T17:35:17-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577687-map_longest-and-map_strict/ <p style="color: grey"> Python recipe 577687 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/iteration/">iteration</a>, <a href="/recipes/tags/map/">map</a>). Revision 2. </p> <p>In Python 3, the map builtin silently drops any excess items in its input:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; a = [1, 2, 3] &gt;&gt;&gt; b = [2, 3, 4] &gt;&gt;&gt; c = [3, 4, 5, 6, 7] &gt;&gt;&gt; list(map(lambda x,y,z: x*y+z, a, b, c)) [5, 10, 17] </code></pre> <p>In Python 2, map pads the shorter items with None, while itertools.imap drops the excess items. Inspired by this, and by itertools.zip_longest, I have map_longest that takes a value to fill missing items with, and map_strict that raises an exception if a value is missing.</p> <pre class="prettyprint"><code>&gt;&gt;&gt; list(map_longest(lambda x,y,z: x*y+z, a, b, c, fillvalue=0)) [5, 10, 17, 6, 7] &gt;&gt;&gt; list(map_strict(lambda x,y,z: x*y+z, a, b, c)) Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "&lt;stdin&gt;", line 9, in map_strict ValueError: too few items in iterable </code></pre> Planet Terrain Heightmap Generator (Python) 2010-03-10T16:35:37-08:00Shea Kauffmanhttp://code.activestate.com/recipes/users/4168682/http://code.activestate.com/recipes/576929-planet-terrain-heightmap-generator/ <p style="color: grey"> Python recipe 576929 by <a href="/recipes/users/4168682/">Shea Kauffman</a> (<a href="/recipes/tags/creation/">creation</a>, <a href="/recipes/tags/generation/">generation</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/heightmap/">heightmap</a>, <a href="/recipes/tags/landscape/">landscape</a>, <a href="/recipes/tags/map/">map</a>, <a href="/recipes/tags/pil/">pil</a>, <a href="/recipes/tags/planet/">planet</a>, <a href="/recipes/tags/planted/">planted</a>, <a href="/recipes/tags/pygame/">pygame</a>, <a href="/recipes/tags/terrain/">terrain</a>, <a href="/recipes/tags/world/">world</a>). Revision 9. </p> <p>The process is simply: 1. Take a plane 2. Cut out a shape 3. Make it a little taller 4. Repeat</p> <ul> <li>Similar to the <a href="http://freespace.virgin.net/hugo.elias/models/m_landsp.htm">spherical landscape</a> algorithm by Hugo Elias.</li> <li>I found a combination of Ovals and Triangles to produce the best results.</li> </ul>