pythonic way to find separate integer intervals in dictionary integer mapping
<p>I have a dictionary mapping one integer range onto another, as an example:</p>

<pre><code>data = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 6, 8: 6, 9: 7, 10: 8}
</code></pre>

<p>I want to find all regions where the numeric intervals in the values fail to increment, so my output should be:</p>

<pre><code>result = [[6, 8]]
</code></pre>

<p>For the above input.</p>

<p>I think my answer will involve the grouping itertools recipe described <a href="https://docs.python.org/2.6/library/itertools.html#examples" rel="nofollow">here</a>. I tried to do a simple implementation of it for my purposes, but I am unsure how to transform the results into my desired result:</p>

<pre><code>from operator import itemgetter
from itertools import groupby
data = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 6, 8: 6, 9: 7, 10: 8}
for k, pos in groupby(enumerate(data.iteritems()), lambda (i, (a, s)):i - s):
    print map(itemgetter(1), pos)
</code></pre>

<p>Has the output:</p>

<pre><code>[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]
[(7, 6)]
[(8, 6), (9, 7), (10, 8)]
</code></pre>

<p>Which properly groups my input data, but does not really give me the interval I want. The challenge I see in parsing this output to the interval I want is that it is giving me an interval for the 'skip region'.</p>

<p>What about something like that:</p>

<pre><code>data = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 6, 8: 6, 9: 7, 10: 8}
sd = sorted(data.items())
result = []
for i in range(1, len(sd)):
    if sd[i][1] == sd[i-1][1]:
        result.append(i)
</code></pre>

<p>I think <code>sorted(data.items())</code> is both clearer and more pythonic that your loop</p>

<p>This gives the result:</p>

<pre><code>print result
[7, 8]
</code></pre>

<p>That might be a good start ?</p>

python