Danya
Danya

Reputation: 77

How to count same strings in numpy array?

I have a numpy array from strings and I want to count the same strings. Is it possible? Here is my code:

import numpy as np
import sys
arr = np.array(sys.stdin.read().split(), dtype = '>U20')
print(arr)

Upvotes: 1

Views: 1639

Answers (1)

Vikas Periyadath
Vikas Periyadath

Reputation: 3186

Try this :

import collections, numpy

collections.Counter(arr)

Or

unique, counts = numpy.unique(arr, return_counts=True)
dict(zip(unique, counts))

Upvotes: 4

Related Questions