擅长:python、mysql、java
<p>不漂亮,但很管用。我不喜欢最后一个循环,buy想不出没有它的方法:</p>
<pre><code>ab = np.vstack((a,b))
ab.sort(axis=0)
join_with_next = ab[1:, 0] - ab[:-1, 1] <= 1
endpoints = np.concatenate(([0],
np.where(np.diff(join_with_next) == True)[0] + 2,
[len(ab,)]))
lengths = np.diff(endpoints)
new_lengths = lengths.copy()
if join_with_next[0] == True:
new_lengths[::2] = 1
else:
new_lengths[1::2] = 1
new_endpoints = np.concatenate(([0], np.cumsum(new_lengths)))
print endpoints, lengths
print new_endpoints, new_lengths
starts = endpoints[:-1]
ends = endpoints[1:]
new_starts = new_endpoints[:-1]
new_ends = new_endpoints[1:]
c = np.empty((new_endpoints[-1], 2), dtype=ab.dtype)
for j, (s,e,ns,ne) in enumerate(zip(starts, ends, new_starts, new_ends)):
if e-s != ne-ns:
c[ns:ne] = np.array([np.min(ab[s:e, 0]), np.max(ab[s:e, 1])])
else:
c[ns:ne] = ab[s:e]
>>> c
array([[ 2, 18],
[20, 29],
[33, 35]])
</code></pre>