[Shootout-list] update to Python word-frequency program

Steven Bethard Steven Bethard <steven.bethard@gmail.com>
Wed, 16 Mar 2005 00:54:20 -0700


------=_Part_20_9839812.1110959660388
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Looks like the Python word-frequency program isn't taking advantage of
some of the niceties of Python 2.4.  I've attached an updated version.
 It doesn't appear to run any slower for me, and is substantially more
concise.

Steve
-- 
You can wordify anything if you just verb it.
        --- Bucky Katt, Get Fuzzy

------=_Part_20_9839812.1110959660388
Content-Type: text/x-python; name="wc.py"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment; filename="wc.py"

import sys

def main():
    i_r =3D map(chr, range(256))

    trans =3D [' '] * 256
    o_a, o_z =3D ord('a'), (ord('z')+1)
    trans[ord('A'):ord('Z')+1] =3D i_r[o_a:o_z]
    trans[o_a:o_z] =3D i_r[o_a:o_z]
    trans =3D ''.join(trans)

    count =3D {}
    for line in sys.stdin:
        for word in line.translate(trans).split():
            try:
                count[word] +=3D 1
            except KeyError:
                count[word] =3D 1

    l =3D sorted(zip(count.itervalues(), count.iterkeys()), reverse=3DTrue)

    print '\n'.join(["%7s %s" % (count, word) for count, word in l])
   =20
main()
------=_Part_20_9839812.1110959660388--