I've been having a hard time finding an answer for this online, so I thought I'd query good ol' Stack Overflow.
I have this example:
v = np.array([
np.array([1, 1]),
np.array([1, 2]),
np.array([1, 3]),
np.array([1, 4]),
np.array([1, 5]),
np.array([2, 1]),
np.array([2, 2]),
np.array([2, 3]),
np.array([3, 1]),
np.array([3, 2]),
np.array([3, 3]),
np.array([3, 4]),
np.array([4, 1]),
np.array([4, 2]),
np.array([4, 3]),
np.array([4, 4]),
np.array([4, 5]),
np.array([4, 6]),
])
k = np.split(v[:, 1], np.unique(v[:, 0], return_index=True)[1][1:])
# output below
[
np.array([1,2,3,4,5]),
np.array([1,2,3]),
np.array([1,2,3,4]),
np.array([1,2,3,4,5,6])
]
My aim is to select the first and last element of each array in the output list. What I'd like to do is something like:
k = np.array(k, dtype=object)
new_k = (([:, 0], [:, -1]))
But alas, this is not possible. Maybe there is a way to rewrite the line that creates k
to just have the first and last item?
Notice that I am trying to accomplish this with no list comprehension, defining functions, or loops - just "vanilla" numpy. If that's not feasible, any direction toward the next most efficient way of accomplishing this would be great.