Popular recipes tagged "spread"http://code.activestate.com/recipes/tags/spread/2015-01-12T22:16:37-08:00ActiveState Code RecipesEqually-spaced numbers (linspace) (Python) 2015-01-12T22:16:37-08:00Andrew Barnerthttp://code.activestate.com/recipes/users/4184316/http://code.activestate.com/recipes/579000-equally-spaced-numbers-linspace/ <p style="color: grey"> Python recipe 579000 by <a href="/recipes/users/4184316/">Andrew Barnert</a> (<a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/linspace/">linspace</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/spread/">spread</a>). </p> <p>An equivalent of <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html"><code>numpy.linspace</code></a>, but as a pure-Python lazy sequence.</p> <p>Like NumPy's <code>linspace</code>, but unlike the <a href="http://code.activestate.com/recipes/577068/"><code>spread</code></a> and <a href="http://code.activestate.com/recipes/577068/"><code>frange</code></a> recipes listed here, the <code>num</code> argument specifies the number of values, not the number of intervals, and the range is closed, not half-open.</p> <p>Although this is primarily designed for floats, it will work for <code>Fraction</code>, <code>Decimal</code>, NumPy arrays (although this would be silly) and even <code>datetime</code> values.</p> <p>This recipe can also serve as an example for creating lazy sequences.</p> <p>See the discussion below for caveats.</p> Generate equally-spaced floats (Python) 2011-09-24T18:49:39-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577878-generate-equally-spaced-floats/ <p style="color: grey"> Python recipe 577878 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/spread/">spread</a>). </p> <p>Generating a list of equally-spaced floats can surprising due to floating point rounding. See, for example, the recipe for a <a href="http://code.activestate.com/recipes/577068">floating point range</a>. One way of avoiding some surprises is by changing the API: instead of specifying a start, stop and step values, instead use a start, stop and count:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; list(spread(0.0, 2.1, 7)) [0.0, 0.3, 0.6, 0.9, 1.2, 1.5, 1.8] </code></pre> <p>Like <a href="http://code.activestate.com/recipes/577068">frange</a> <code>spread</code> takes an optional mode argument to select whether the start and end values are included. By default, start is included and end is not, and exactly count values are returned.</p> Equally-spaced floats part 2 (Python) 2011-09-27T15:48:00-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577881-equally-spaced-floats-part-2/ <p style="color: grey"> Python recipe 577881 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/spread/">spread</a>). </p> <p>See the recipe <a href="http://code.activestate.com/recipes/577878">here</a> for a description of the problem.</p> <p>This variant allows the user to choose between two APIs, either by supplying the count, start value and end value:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; list(spread(5, 1.0, end=2.0)) [1.0, 1.2, 1.4, 1.6, 1.8] </code></pre> <p>or the count, start value and step size:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; list(spread(5, 1.0, step=-0.25)) [1.0, 0.75, 0.5, 0.25, 0.0] </code></pre> <p>As before, the count argument specifies how many subdivisions are made. The optional argument <code>mode</code> selects whether the start and end values are included. By default, start is included and end is not, and exactly count values are returned.</p>