'Get the unique value of the different part of an array

I have an array with two rows, each rows repeated 4 columns.

  a = np.array([[ 0,  0, 0,  0,  4,  4,  4,  4,  7,  7,  7, 7, 1, 1,  1,  1],
   [ 10,  10, 10,  10,  14,  14,  14,  14,  17,  17,  17, 17, 21, 21,  21,  21]])

I want to consider one value for 4 columns. For example, 0 for the 4 columns of the first row. I can not use the unique(), The output of a is:

b = np.array([[ 0,4, 7, 1],
   [ 10,14, 17,  21]])


Solution 1:[1]

You can remove duplicates in a row

def remove_duplicates(arr):
    """
    remove duplicates in a row from array
    """
    if len(arr) == 0:
        return arr
    else:
        i = 0
        while i < len(arr) - 1:
            if arr[i] == arr[i + 1]:
                del arr[i]
            else:
                i += 1
        return arr
print(remove_duplicates([0,0,0,0,1,1,1,1,0,0,0,0]))
[0, 1, 0]

print(remove_duplicates([0,0,0,0,4,4,4,4,7,7,7,7,1,1,1,1]))
[0, 4, 7, 1]

Solution 2:[2]

You can simply take every 4th column like so:

>>> a = np.array([[ 0,  0, 0,  0,  4,  4,  4,  4,  7,  7,  7, 7, 1, 1,  1,  1],
...    [ 10,  10, 10,  10,  14,  14,  14,  14,  17,  17,  17, 17, 21, 21,  21,  21]])
>>> a[:,::4]
array([[ 0,  4,  7,  1],
       [10, 14, 17, 21]])

For more info, see numpy slicing.

Solution 3:[3]

Use np.apply_along_axis, which applies a method across each row:

>>> np.apply_along_axis(lambda x: x[::4], axis=1, arr=a)
array([[ 0,  4,  7,  1],
       [10, 14, 17, 21]])

Here, the function we pass in just takes every 4th element of the row (this assumes 4 is always static).

Solution 4:[4]

You could use itertools.groupby:

>>> import numpy as np
>>> from itertools import groupby
>>> a = np.array([[0, 0, 0, 0, 4, 4, 4, 4, 7, 7, 7, 7, 1, 1, 1, 1], [10, 10, 10, 10, 14, 14, 14, 14, 17, 17, 17, 17, 21, 21, 21, 21]])
>>> a
array([[ 0,  0,  0,  0,  4,  4,  4,  4,  7,  7,  7,  7,  1,  1,  1,  1],
       [10, 10, 10, 10, 14, 14, 14, 14, 17, 17, 17, 17, 21, 21, 21, 21]])
>>> b = np.array([[k for k, _ in groupby(arr)] for arr in a])
>>> b
array([[ 0,  4,  7,  1],
       [10, 14, 17, 21]])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 PleSo
Solution 2
Solution 3 TerryA
Solution 4 Sash Sinha