cataloging words in text file

Aahz Maruch aahz at panix.com
Fri Mar 2 19:08:57 EST 2001


In article <v9Vn6.396$z6.33544 at ruti.visi.com>,
Grant Edwards <grante at visi.com> wrote:
>
>#!/usr/local/bin/python2.1
>import sys
>d={}
>for w in sys.stdin.read().split():
>    if d.has_key(w):
>        d[w] += 1
>    else:
>        d[w] = 1
>print d    

Since you're using 2.1, try this

for w in sys.stdin.read().split():
    d[w] = d.setdefault(w,0) + 1
-- 
                      --- Aahz (Copyright 2001 by aahz at pobox.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

Nostalgia just ain't what it used to be



More information about the Python-list mailing list