reducing list based on fuzzy values in python
<p>I have a list that contains groups of nearly identical numeric values. i.e. (1004.523, 1004.575, 1004.475, 791.385, 791.298, 791.301, 791.305, 791.299)</p>

<p>What I am trying to do is read through the list and find all the 1004.5 +- values aggregate them and find the average value. then continue on and find all the 791.0 +- values and do the same to them. </p>

<p>I do not know how many individual values there will be in each "group" nor do I know how many groups there will be.</p>

<p>The result I am looking for is another list which would contain the average values of each of the groups. So in the example my result would be (1004.524, 791.3176)</p>

<p>The code I'm currently using is very Kludgey and it seems there should be a much better way to do it.</p>

<p>As you can see I have to repeat code twice once in the else and once at the end of the if since the last set of numbers does not trigger the else. Plus at the completion of the if I need to add the last value.</p>

<p>If I use the len(tones) rather that len(tones)-i I get an out of range error.</p>

<p>Any thoughts or suggestions would be appreciated. Thanks for your help.</p>

<p>Ed</p>

<pre><code>    toneLen = len(tones) -1
    for i in range(0, toneLen):
        if abs(tones[i]-tones[i+1]) &lt;= 2.0:
            tmpTones.append(tones[i])
        else:
            freq = mean(tmpTones)
            newTones.append(freq)
            tmpTones = []
    tmpTones.append(tones[i+1])
    freq = mean(tmpTones)
    newTones.append(freq)                
    tones = newTones
</code></pre>

<p><strong>UPDATE:</strong>
First I wanted to thank everyone who submitted suggestions. The response was very quick and helpful. I should have probably included some more info which I am doing below. Thanks so much for your help.</p>

<p>Second , a quick explanation of what I am trying to do. Our local Fire Department is looking for a way to track dispatches for departments close to them. For the most part they use two tone sequential paging i.e. 1000Hz followed by 500Hz.</p>

<p>So I am using numpy fft to find the tone frequency. Since the accuracy of the tone appears to be about +- 2 Hz, I compare the calculated frequency to a list of known paging tones and pick the closest match. After all the tones have been matched to the paging tones I look for matches to departments of interest. </p>

<p>One thing I did not know when I started this that in any given dispatch the same tone can be repeated several times, so the order of the tones is important. An example:
707.3, 339.6, 707.3, 569.1, 447.2, 569.1 would be a typical dispatch. I then look to see if any of the tone pairs are ones I'm interested in if so I display a message </p>

<p>Thanks again for all your help.</p>

<p>Ed</p>
