Counting

rockmode at gmail.com rockmode at gmail.com
Sun Apr 29 16:16:15 EDT 2007


That's a short, abridged version of my code :) But, what I want is to
count total# of keywords per line and print 'em. Rather than
printing :

The word 'and' belongs in line num: 1
The word 'del' belongs in line num: 1
The word 'from' belongs in line num: 1

I want to print " Line #1 has 3 keywords"

;)


> You probably want something that goes a little like this:
>
> for i,line in enumerate(linelist):
>    for k in line.split():
>      if keyword.iskeyword(k):
>        total += line.count(k)
>        print "The word '%s' belongs in line num: %d" % (k, i+1)
>
> print "Total keyords are: %d" % total
>
> James





More information about the Python-list mailing list