python normal distribution
<p>I have a list of numbers, with sample mean and SD for these numbers. Right now I am trying to find out the numbers out of mean+-SD,mean +-2SD and mean +-3SD.
For example, in the part of mean+-SD, i made the code like this:</p>

<pre><code>ND1 = [np.mean(l)+np.std(l,ddof=1)]    
ND2 = [np.mean(l)-np.std(l,ddof=1)]

m=sorted(l)

print(m)

ND68 = []

if ND2 &gt; m and m&lt; ND1:

    ND68.append(m&lt;ND2 and m&gt;ND1)
    print (ND68)
</code></pre>

<p>Here is my question:
1. Could number be calculated by the list and arrange. If so, which part I am doing wrong.  Or there is some package I can use to solve this.</p>

<p>You are on the right track there. You know the mean and standard deviation of your list <code>l</code>, though I'm going to call it something a little less ambiguous, say, <code>samplePopulation</code>. </p>

<p>Because you want to do this for several intervals of standard deviation, I recommend crafting a small function. You can call it multiple times without too much extra work. Also, I'm going to use a <a href="https://docs.python.org/3.5/tutorial/datastructures.html#list-comprehensions" rel="nofollow">list comprehension</a>, which is just a <code>for</code> loop in one line. </p>

<pre><code>import numpy as np

def filter_by_n_std_devs(samplePopulation, numStdDevs):
    # you mostly got this part right, no need to put them in lists though
    mean = np.mean(samplePopulation) # no brackets needed here
    std = np.std(samplePopulation) # or here
    band = numStdDevs * std 

    # this is the list comprehension
    filteredPop = [x for x in samplePopulation if x &lt; mean - band or x &gt; mean + band]
    return filteredPop

# now call your function with however many std devs you want
filteredPopulation = filter_by_n_std_devs(samplePopulation, 1)
print(filteredPopulation)
</code></pre>

<p>Here's a translation of the list comprehension (based on your use of <code>append</code> it looks like you may not know what these are, otherwise feel free to ignore).</p>

<pre><code># remember that you provide the variable samplePopulation
# the above list comprehension
filteredPop = [x for x in samplePopulation if x &lt; mean - band or x &gt; mean + band]

# is equivalent to this:
filteredPop = []
for num in samplePopulation:
    if x &lt; mean - band or x &gt; mean + band:
        filteredPop.append(num)
</code></pre>

<p>So to recap: </p>

<ul>
<li>You don't need to make a list object out of your mean and std calculations</li>
<li>The function call let's you plug in your <code>samplePopulation</code> and any number of standard deviations you want without having to go in and manually change the value</li>
<li>List comprehensions are one line for loops, more or less, and you can even do the filtering you want right inside it! </li>
</ul>

python python-3.x