0

Possible Duplicate:
item frequency count in python

Quick question

How do you find how many times a word appears in an array?

I have an array that has around 5000 words of text, and i want to find how many times the word "help" appears in the array. How do i do this?

the array is stored in x, so my code looks like this:

x = [...]
word = "help"

and then i dont know what to put to get the number of times "help" appears in x

thank you for any help!

Community
  • 1
  • 1
Hoops
  • 855
  • 3
  • 14
  • 25

3 Answers3

6
>>> import collections
>>> print collections.Counter(['a', 'word', 'is', 'a', 'thing', 'that', 'is', 'countable'])
Counter({'a': 2, 'is': 2, 'word': 1, 'that': 1, 'countable': 1, 'thing': 1})

This is 2.7+, a Counter.

Based on your edit, where each element in the list is a letter instead of the full word, then:

>>> import re
>>> letters = 
['i', 'n', 'e', 'e', 'd', 's', 'o', 'm', 'e', 'h', 'e', 'l', 'p', 'h', 'e', 'l', 'p', 'm', 'e', 'p', 'l', 'e', 'a', 's', 'e', 'I', 'n', 'e', 'e', 'd', 'h', 'e', 'l', 'p']
>>> len(re.findall('help', "".join(letters)))
3
sberry
  • 113,858
  • 17
  • 127
  • 157
1

As @sberry has depicted, Counter would server the purpose, but in case you are only searching a single word once and not interested to get the occurrence of all the words, you can use a simpler tool for the purpose

(I have taken the example from sberry)

Given a list of words to find the occurrence of any given words, you can use the count method of the list

>>> list_of_words=['a', 'word', 'is', 'a', 'thing', 'that', 'is', 'countable']
>>> list_of_words.count('is')
2

As your comments have shown you may be interested to search on a list of characters. Such as

letters =
['i', 'n', 'e', 'e', 'd', 's', 'o', 'm', 'e', 'h', 'e', 'l', 'p', 'h', 'e', 'l', 'p', 'm', 'e', 'p', 'l', 'e', 'a', 's', 'e', 'I', 'n', 'e', 'e', 'd', 'h', 'e', 'l', 'p']

You can also use the count on the string after is generated by concatenating all the characters

>>> ''.join(letters).count('help')
3

In case the words are jumbled, collections.Counter ad do magic here

>>> def count_words_in_jumbled(jumbled,word):
    jumbled_counter = collections.Counter(jumbled)
    word_counter = collections.Counter(word)
    return min(v /word_counter[k] for k,v in jumbled_counter.iteritems() if k in word)

>>> count_words_in_jumbled(['h','e','l','l','h','e','l','l','h','e','l'],'hel')
3
>>> count_words_in_jumbled(['h','e','l','l','h','e','l','l','h','e','l'],'hell')
2
>>> count_words_in_jumbled(['h','x','e','y','l','u','p'] ,'help')
1
Abhijit
  • 55,716
  • 14
  • 105
  • 186
0
nhelps = len(''.join(charlist).split('help')[1:]