Home > Software design >  Numpy, How to count how many times a string occurs
Numpy, How to count how many times a string occurs

Time:04-08

Hi I am trying to count how many times a word appears in a textfile that I am loading in. I have loaded it in using the follow code

textFile = np.loadtxt("student.txt", skiprows=2, dtype='str')
students = np.array(textFile)

Currently I am using the following to try and count the occurrences but it only seems to return their position in the array.

amountWales = np.core.defchararray.count(students, 'Wales')
print("The amount of students attending school in Wales is: \n", amountWales)

Would anyone have any ideas on how to just print the amount of times they occur rather than their position

My current output is:

The amount of students attending school in Wales is: 
 [[0 0 0 0 1]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 1]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 1]
 [0 0 0 0 0]]

CodePudding user response:

If you need to use numpy (not my first choice for this task), one way you can use it to count the number of times each country appears in a text file is

import numpy as np

# load data from text file using a comma as delimiter, making the datatype
# of resulting array unicode strings and stripping these strings of whitespace
textfile = np.genfromtxt('blankpaper.txt', delimiter=',', dtype = 'U',
    autostrip = True)

# dictionary to store occurrences of each country
counts = {'Wales': 0, 'Scotland': 0, 'England': 0}
# use numpy to count occurrences of each country in the array
for key in counts:
    counts[key] = np.count_nonzero(textfile == key)

print(textfile)
print()
print(counts)

Input ("blankpaper.txt")


B123, Jones, Barry, 24, Wales
B134, Kerry, Jane, 21, Scotland
B456, Smith, Percy, 19, England
B999, Smith, Jack, 19, England

Output


[['B123' 'Jones' 'Barry' '24' 'Wales']
 ['B134' 'Kerry' 'Jane' '21' 'Scotland']
 ['B456' 'Smith' 'Percy' '19' 'England']
 ['B999' 'Smith' 'Jack' '19' 'England']]

{'Wales': 1, 'Scotland': 1, 'England': 2}

I hope this helps. Let me know if there are any questions!

CodePudding user response:

Like I said in the comments load the file as a string.

string = "this person says this cannot be founnd online, But I found this regardless"
print("Output: ", string.count("this"))

OUTPUT:

Output:  3

CodePudding user response:

Can't you just do something like

amountWales = np.count_nonzero(students == 'Wales')

(untested)

  • Related