Find a certain difference between members of a list (or set) of numbers
<p>my first question, so please be gentle, I hope I get the formatting right :) I think the question is self explaining. I am looking for a better/faster way to find a difference in a set of numbers... maybe I want a tolerance with it. All I came up with is:</p>

<pre><code> def difference(numbers,diff,tol):
    '''diff is the searched difference,numbers is a list \
    of numbers and tol the tolerance'''
    numbers.sort()
    match=set()
    for i in numbers:
        low = i+diff-tol
        high= i+diff+tol
        for k in numbers:
                if k &gt; high:
                    break
                if k &lt; low:
                    continue
                match.add(i)
                match.add(k)
    return match
</code></pre>

<p>But I bet there are way better ways to achieve the result.</p>

<p>Any idea is welcome,</p>

<p>Christian</p>

<pre><code>    count    = len(numbers)
    numbers1 = numbers[:count - 1]
    numbers2 = numbers[1:]

    for i in range(0, count - 1):
        dif = numbers2[i] - numbers1[i]
        if abs(dif) &lt;= tol:
            match.add(numbers1[i])
            match.add(numbers2[i])
</code></pre>

python