Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>(New Answer) Using Numpy</h1> <pre><code>ranges = np.vstack((a,b)) ranges.sort(0) # List of non-overlapping ranges nonoverlapping = (ranges[1:,0] - ranges[:-1,1] &gt; 1).nonzero()[0] # Starts are 0, and all the starts not overlapped by their predecessor starts = np.hstack(([0], nonoverlapping + 1)) # Ends are -1 and all the ends who aren't overlapped by their successor ends = np.hstack(( nonoverlapping, [-1])) # Result result = np.vstack((ranges[starts, 0], ranges[ends, 1])).T </code></pre> <p><strong>(Old answer) Using lists and sets</strong></p> <pre><code>import numpy as np import itertools def ranges(s): """ Converts a list of integers into start, end pairs """ for a, b in itertools.groupby(enumerate(s), lambda(x, y): y - x): b = list(b) yield b[0][1], b[-1][1] def intersect(*args): """ Converts any number of numpy arrays containing start, end pairs into a set of indexes """ s = set() for start, end in np.vstack(args): s = s | set(range(start,end+1)) return s a = np.array([[5,7],[12, 18],[20,29]]) b = np.array([[2,4],[8,11],[33,35]]) result = np.array(list(ranges(intersect(a,b)))) </code></pre> <h1>References</h1> <ul> <li><a href="https://stackoverflow.com/questions/6821156/how-to-find-range-overlap-in-python">How to find range overlap in python?</a></li> <li><a href="http://docs.python.org/2/library/sets.html" rel="nofollow noreferrer">http://docs.python.org/2/library/sets.html</a></li> <li><a href="https://stackoverflow.com/questions/4628333/converting-a-list-of-integers-into-range-in-python">converting a list of integers into range in python</a></li> <li><a href="http://docs.python.org/2/library/itertools.html#itertools.groupby" rel="nofollow noreferrer">http://docs.python.org/2/library/itertools.html#itertools.groupby</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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