quick question

John Hunter jdhunter at ace.bsd.uchicago.edu
Wed Nov 20 01:24:20 EST 2002


>>>>> "Cousin" == Cousin Stanley <CousinStanley at HotMail.com> writes:


After all that discussion, I thought it would be worthwhile to wrap up
with a class that does counting of items in a sequence (like icecream
votes!) that takes into account the results of your timing routines.
The code should perform as well as any we've discussed, and has a
simple interface.

Although, the example below tabulates character frequencies in english
words (useful for crypt-analysis of Caesar ciphers), it would work on
as well on a file of votes.  Just do

  c = counter()
  c.count(file('myfile.dat'))

from __future__ import division

class counter(dict):
    "Count the number of occurances in a sequence (or several seqs)"

    def total(self):
        return reduce(int.__add__, self.values())

    def count(self, seq):
        for x in seq:
            self[x] = self.get(x,0)+1

    def sort(self):
        "return a list of (count, item) pairs sorted by decreasing count"
        x = [(count, item) for (item, count) in self.items()]
        x.sort()
        x.reverse()
        return x

# compute the frequency of characters in english words    
s  = file('/usr/share/dict/linux.words').read()
c = counter()
c.count(s)
total = c.total()

for (count, letter) in c.sort():
    print '%s\t%%%1.2f' % (letter, 100.0*count/total)


This must be in the cookbook already.....

John Hunter




More information about the Python-list mailing list