Created
August 9, 2019 01:54
-
-
Save iphysresearch/f3dfdc19545627bb7970eb8e078bf410 to your computer and use it in GitHub Desktop.
Group Neighbor Elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def GroupNeighbor(l): | |
""" | |
Eg: Input <= np.array | |
l = np.array([58,59, | |
87,98, | |
101,102,103,104, | |
111,122, | |
133,134,135,136]) | |
GroupNeighbor(l) = [[58, 59], | |
[101, 102, 103, 104], | |
[133, 134, 135, 136]] | |
""" | |
NearIndex = lambda ll: np.array([value -ll[index-1] for index, value in enumerate(ll) if index != 0]) | |
ReshapeIndex = lambda ll: np.append(0, ll +1) | |
AppendZero = lambda ll: np.append(ll,0) | |
AppendOne = lambda ll: np.append(ll,ll[-1]+1) | |
u = np.argwhere(NearIndex(l) == 1)[:,0] | |
t = ReshapeIndex(np.argwhere(NearIndex(AppendZero(u)) != 1)[:,0]) | |
return [ l[AppendOne(index)] for index in [u[t[i]:t[i+1]] for i in range(t.size-1)] ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment