1

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)
4
  • What do you mean "count the strings"? Do you want len(arr)? Commented Apr 3, 2018 at 12:35
  • I believe what you are looking for is collections.Counter(). Commented Apr 3, 2018 at 12:36
  • I mean the number of the same words. Commented Apr 5, 2018 at 10:58
  • I have a array from words and it has many same words, I need to count them Commented Apr 5, 2018 at 11:00

1 Answer 1

4

Try this :

import collections, numpy

collections.Counter(arr)

Or

unique, counts = numpy.unique(arr, return_counts=True)
dict(zip(unique, counts))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it's all I need

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.