Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're using one element lists; If you want to perform that calculation specifically, just remove the braces. I'll assume that you actually do have multi-valued lists. A reasonable solution is to combine <code>map()</code>, which applies a function to each element in one or more lists, as well as some of the functions from the <code>operator</code> module, which turn many python operators (like <code>+</code> and <code>-</code>) into functions.</p> <p>First well just set up some lists.</p> <pre><code>&gt;&gt;&gt; import random &gt;&gt;&gt; d1 = [random.randrange(10) for ignored in range(10)] &gt;&gt;&gt; d2 = [random.randrange(10) for ignored in range(10)] &gt;&gt;&gt; c1 = [random.randrange(10) for ignored in range(10)] &gt;&gt;&gt; c2 = [random.randrange(10) for ignored in range(10)] &gt;&gt;&gt; c1 [1, 1, 7, 5, 5, 7, 4, 0, 7, 2] &gt;&gt;&gt; c2 [9, 2, 7, 7, 1, 1, 9, 3, 6, 8] &gt;&gt;&gt; d1 [0, 3, 4, 8, 9, 0, 7, 1, 6, 5] &gt;&gt;&gt; d2 [3, 9, 5, 2, 1, 9, 2, 7, 9, 5] </code></pre> <p>Next we just replace each of your operations into a <code>map</code> call to the corresponding <code>operator.*</code></p> <pre><code>&gt;&gt;&gt; import operator &gt;&gt;&gt; x = map(operator.sub, d2, c2) &gt;&gt;&gt; y = map(operator.sub, d2, c2) &gt;&gt;&gt; z = map(operator.mul, x, x) &gt;&gt;&gt; w = map(operator.mul, y, y) &gt;&gt;&gt; import math &gt;&gt;&gt; answer = map(math.sqrt, map(operator.add, z, w)) &gt;&gt;&gt; print answer [8.48528137423857, 9.899494936611665, 2.8284271247461903, 7.0710678118654755, 0.0, 11.313708498984761, 9.899494936611665, 5.656854249492381, 4.242640687119285, 4.242640687119285] &gt;&gt;&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload