In [1]:
import numpy as np

In [2]:
a = np.array([0, 0, 30, 10, 10, 20])
print(a)


[ 0  0 30 10 10 20]

In [3]:
print(np.unique(a))


[ 0 10 20 30]

In [4]:
print(type(np.unique(a)))


<class 'numpy.ndarray'>

In [5]:
l = [0, 0, 30, 10, 10, 20]
print(l)


[0, 0, 30, 10, 10, 20]

In [6]:
print(np.unique(l))


[ 0 10 20 30]

In [7]:
print(type(np.unique(l)))


<class 'numpy.ndarray'>

In [8]:
print(np.unique(a).size)


4

In [9]:
print(len(np.unique(a)))


4

In [10]:
u, counts = np.unique(a, return_counts=True)
print(u)


[ 0 10 20 30]

In [11]:
print(counts)


[2 2 1 1]

In [12]:
print(u[counts == 1])


[20 30]

In [13]:
print(u[counts != 1])


[ 0 10]

In [14]:
print(np.unique(a, return_counts=True))


(array([ 0, 10, 20, 30]), array([2, 2, 1, 1]))

In [15]:
print(type(np.unique(a, return_counts=True)))


<class 'tuple'>

In [16]:
u, indices = np.unique(a, return_index=True)
print(u)


[ 0 10 20 30]

In [17]:
print(indices)


[0 3 5 2]

In [18]:
print(a)


[ 0  0 30 10 10 20]

In [19]:
print(a[indices])


[ 0 10 20 30]

In [20]:
u, inverse = np.unique(a, return_inverse=True)
print(u)


[ 0 10 20 30]

In [21]:
print(inverse)


[0 0 3 1 1 2]

In [22]:
print(a)


[ 0  0 30 10 10 20]

In [23]:
print(u[inverse])


[ 0  0 30 10 10 20]

In [24]:
u, indices, inverse, counts = np.unique(a, return_index=True, return_inverse=True, return_counts=True)
print(u)


[ 0 10 20 30]

In [25]:
print(indices)


[0 3 5 2]

In [26]:
print(inverse)


[0 0 3 1 1 2]

In [27]:
print(counts)


[2 2 1 1]

In [28]:
print(np.unique(a, return_counts=True, return_index=True, return_inverse=True))


(array([ 0, 10, 20, 30]), array([0, 3, 5, 2]), array([0, 0, 3, 1, 1, 2]), array([2, 2, 1, 1]))

In [29]:
a_2d = np.array([[20, 20, 10, 10], [0, 0, 10, 30], [20, 20, 10, 10]])
print(a_2d)


[[20 20 10 10]
 [ 0  0 10 30]
 [20 20 10 10]]

In [30]:
print(np.unique(a_2d))


[ 0 10 20 30]

In [31]:
print(np.unique(a_2d, axis=0))


[[ 0  0 10 30]
 [20 20 10 10]]

In [32]:
print(np.unique(a_2d, axis=1))


[[10 10 20]
 [10 30  0]
 [10 10 20]]

In [33]:
print(a_2d[0])


[20 20 10 10]

In [34]:
print(np.unique(a_2d[0]))


[10 20]

In [35]:
print(a_2d[:, 2])


[10 10 10]

In [36]:
print(np.unique(a_2d[:, 2]))


[10]

In [37]:
print([np.unique(row) for row in a_2d])


[array([10, 20]), array([ 0, 10, 30]), array([10, 20])]

In [38]:
print([np.unique(row).tolist() for row in a_2d])


[[10, 20], [0, 10, 30], [10, 20]]

In [39]:
print([np.unique(row).size for row in a_2d])


[2, 3, 2]

In [40]:
print(a_2d.T)


[[20  0 20]
 [20  0 20]
 [10 10 10]
 [10 30 10]]

In [41]:
print([np.unique(row) for row in a_2d.T])


[array([ 0, 20]), array([ 0, 20]), array([10]), array([10, 30])]

In [42]:
print(a_2d.shape)


(3, 4)

In [43]:
print([np.unique(a_2d[:, i]) for i in range(a_2d.shape[1])])


[array([ 0, 20]), array([ 0, 20]), array([10]), array([10, 30])]

In [44]:
u, indices, inverse, counts = np.unique(a_2d, return_index=True, return_inverse=True, return_counts=True)
print(u)


[ 0 10 20 30]

In [45]:
print(indices)


[4 2 0 7]

In [46]:
print(a_2d.flatten())


[20 20 10 10  0  0 10 30 20 20 10 10]

In [47]:
print(a_2d.flatten()[indices])


[ 0 10 20 30]

In [48]:
print(inverse)


[2 2 1 1 0 0 1 3 2 2 1 1]

In [49]:
print(u[inverse])


[20 20 10 10  0  0 10 30 20 20 10 10]

In [50]:
print(u[inverse].reshape(a_2d.shape))


[[20 20 10 10]
 [ 0  0 10 30]
 [20 20 10 10]]

In [51]:
print(counts)


[2 5 4 1]

In [52]:
u, indices, inverse, counts = np.unique(a_2d, axis=0, return_index=True, return_inverse=True, return_counts=True)
print(u)


[[ 0  0 10 30]
 [20 20 10 10]]

In [53]:
print(indices)


[1 0]

In [54]:
print(a_2d[indices])


[[ 0  0 10 30]
 [20 20 10 10]]

In [55]:
print(inverse)


[1 0 1]

In [56]:
print(u[inverse])


[[20 20 10 10]
 [ 0  0 10 30]
 [20 20 10 10]]

In [57]:
print(counts)


[1 2]

In [58]:
print(a_2d)


[[20 20 10 10]
 [ 0  0 10 30]
 [20 20 10 10]]

In [59]:
u, indices = np.unique(a_2d, return_index=True)
print(u)


[ 0 10 20 30]

In [60]:
print(a_2d.flatten())


[20 20 10 10  0  0 10 30 20 20 10 10]

In [61]:
print(indices)


[4 2 0 7]

In [62]:
print(list(zip(*np.where(a_2d == 0))))


[(1, 0), (1, 1)]

In [63]:
d = {u: list(zip(*np.where(a_2d == u))) for u in np.unique(a_2d)}
print(d)


{0: [(1, 0), (1, 1)], 10: [(0, 2), (0, 3), (1, 2), (2, 2), (2, 3)], 20: [(0, 0), (0, 1), (2, 0), (2, 1)], 30: [(1, 3)]}

In [64]:
print(d[0])


[(1, 0), (1, 1)]

In [65]:
print(d[10])


[(0, 2), (0, 3), (1, 2), (2, 2), (2, 3)]

In [66]:
print(d[20])


[(0, 0), (0, 1), (2, 0), (2, 1)]

In [67]:
print(d[30])


[(1, 3)]

In [68]:
d = {u: list(zip(*np.where(a_2d == u)))
     for u, c in zip(*np.unique(a_2d, return_counts=True)) if c == 1}
print(d)


{30: [(1, 3)]}

In [69]:
d = {u: list(zip(*np.where(a_2d == u)))
     for u, c in zip(*np.unique(a_2d, return_counts=True)) if c <= 2}
print(d)


{0: [(1, 0), (1, 1)], 30: [(1, 3)]}