alandalusi
alandalusi

Reputation: 1145

Whats wrong with my dict?

I am running the following code to count how many times a word occured in a text file.

def print_words(filename):
    f = open(filename, 'rU')
    dict={}
    for line in f:
       words=line.split()
       for word in words:
          if dict.get(word):
              dict[word]=dict[word]+1
          else: 
              dict[word]=1
    ke = sorted(dict.keys())
    for k, v in ke: print k, v

The dictionary file should hold each word and its count. I was able to get that working fine. But I failed to sort the

Upvotes: 0

Views: 139

Answers (5)

Marian
Marian

Reputation: 6257

For a 2.5 and 2.6 compatible solution, use defaultdict:

from collections import defaultdict
counter = defaultdict(int)

for word in f.read().split():
    counter[word] += 1

This returns a dictionary (a subclass actually, but to be used the same way), where you can just look up counter['someword'] (that returns an integer).

How it works: If the requested key does not exist, it is created using the return value of the given function. In this case, int(), which is 0. See also the examples section at the documentation linked above.

Upvotes: 1

volcano
volcano

Reputation: 3582

Depending on what is more important - order or counting - you may use either ordered dictionary or counter dictionary from the collections module

OrderedDict remembers the elements in the order they are added; Counter - counts elements

With the former, you may do something like that

>>> words = open(filename).read().split()
>>> counters = OrderedDict([(word, words.count(word)) 
                            for word in sorted(list(set(words)))])

You will get sorted dictionary with counters - just in 2 lines.

Upvotes: 0

Lauritz V. Thaulow
Lauritz V. Thaulow

Reputation: 50995

Other answers point out the problem. If you're using python 2.7+, here's an easier way to do it, using the Counter class from the collections module.

from collections import Counter

def print_words(filename):
    with open(filename, 'rU') as f:
        counts = Counter(f.read().split())
    for k, v in sorted(counts.items()):
        print k, v

Upvotes: 2

Kurt Stutsman
Kurt Stutsman

Reputation: 4034

sorted(dict.keys()) returns a sorted list of just the keys. Your for loop is incorrectly expecting to find the values in the same list. Try the code below instead:

for k in ke:
    print k, dict[k]

Upvotes: 1

jedwards
jedwards

Reputation: 30210

Instead of

ke = sorted(dict.keys())
for k, v in ke: print k, v

Try:

for k in sorted(dict.keys()):
    print k, dict[k]

sorted(dict.keys()) will return a sorted list of only the keys (since that's all you're giving it).

Upvotes: 1

Related Questions