Is there a simple way to do this in Python?
For example I have:
x = np.array(['a', 'b', 'a', 'b', 'b', 'u'])
Desired outcome:
[2, 3, 2, 3, 3, 1]
Is there a simple way to do this in Python?
For example I have:
x = np.array(['a', 'b', 'a', 'b', 'b', 'u'])
Desired outcome:
[2, 3, 2, 3, 3, 1]
Use np.unique
with return_counts
and return_inverse
enabled:
_, inverse, count = np.unique(x, return_inverse=True, return_count=True)
result = count[inverse]