1

I need to make a function that reads a string and returns a dictionary where the keys are the words in the string and the values are how many times they occur.

This is what I tried:

    def countWords(arg):
        dic = {}
        for i in agr:
            if i in dic:
                dic[i] += 1
            else:
                dic[i] = 1
        return dic

Which only counts how many times a letter appears.

I thought of separating each word into a different position of a list first, but I'm not sure how to or even if that's the right way to go here..

What should I do?

Mary09
  • 35
  • 3

2 Answers2

1

See collections.Counter. This generally considered to be the best solution for this type of problem.

from collections import Counter


def countWords(s):
    return Counter(s.split())

If you don't want to use the collections module, you can use a try...except block.

def countWords(s):
    d = {}
    for word in s.split():
        try:
            d[word] += 1
        except KeyError:
            d[word] = 1
    return d

Yet another alternative is to use the optional default parameter of dict.get().

def countWords(s):
    d = {}
    for word in s.split():
        d[word] = d.get(word, 0) + 1
    return d

As you can see, there are numerous different ways to accomplish this task.

pzp
  • 5,751
  • 1
  • 24
  • 36
  • This suggestion is not appropriate for what the OP is asking, IMO. He's not asking: "how can I count the frequencies of words in a sentence or string". – nbro Jan 07 '16 at 14:21
  • @nbro When I added the second option, bernie's answer had not yet appeared for me. When I saw that he had an earlier timestamp, I removed that part of my answer. My initial suggestion is *the best way* to solve this task. In fact, this task is the most common use case of collections.Counter I've seen. – pzp Jan 07 '16 at 14:24
1

This is a perfect case for a default dict: https://docs.python.org/2/library/collections.html#collections.defaultdict

import collections as co

def countWords(arg):
    dd = co.defaultdict(int) # since we want counts we use int
    for i in arg.split():    # split on whitespace
        dd[i] += 1           # when a new key is encountered the default value is entered
    return dd
mechanical_meat
  • 144,326
  • 21
  • 211
  • 203