Counting

A

Andy

Hi, the file below will print all the keywords in a file and also the
line # of the keyword. What I couldn't figure out is to count those
keywords per line. For example - "Line #1 has 3 keywords"

Can I do like -

total[j] = total[j] + numwords(k)
"Line number %d has %d keywords" % (j, total[j])

Seems sort of "illegal" in Python?



-------------------------------------------------
import keyword, sys, string, fileinput
def numwords(s):
list = string.split(s)
return len(list)

# Get the file name either from the command-line or the user
if len(sys.argv) != 2:
name = raw_input("Enter the file name: ")
else:
name = sys.argv[1]

inp = open(name,"r")
linelist = inp.readlines()
total, words,lines = 0, 0, 0

for i in range(len(linelist)):
line = linelist
tempwords = line.split()
for k in tempwords:
if keyword.iskeyword(k):
total = total + numwords(k)
j = i + 1
print" The word * %s * belongs in line number: %d" % (k,
j)

print "Total keywords in this file are: %d" %(total)
 
J

James Stroud

Andy said:
Hi, the file below will print all the keywords in a file and also the
line # of the keyword. What I couldn't figure out is to count those
keywords per line. For example - "Line #1 has 3 keywords"

Can I do like -

total[j] = total[j] + numwords(k)
"Line number %d has %d keywords" % (j, total[j])

Seems sort of "illegal" in Python?



-------------------------------------------------
import keyword, sys, string, fileinput
def numwords(s):
list = string.split(s)
return len(list)

# Get the file name either from the command-line or the user
if len(sys.argv) != 2:
name = raw_input("Enter the file name: ")
else:
name = sys.argv[1]

inp = open(name,"r")
linelist = inp.readlines()
total, words,lines = 0, 0, 0

for i in range(len(linelist)):
line = linelist
tempwords = line.split()
for k in tempwords:
if keyword.iskeyword(k):
total = total + numwords(k)
j = i + 1
print" The word * %s * belongs in line number: %d" % (k,
j)

print "Total keywords in this file are: %d" %(total)


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
 
J

James Stroud

James said:
Andy said:
Hi, the file below will print all the keywords in a file and also the
line # of the keyword. What I couldn't figure out is to count those
keywords per line. For example - "Line #1 has 3 keywords"

Can I do like -

total[j] = total[j] + numwords(k)
"Line number %d has %d keywords" % (j, total[j])

Seems sort of "illegal" in Python?



-------------------------------------------------
import keyword, sys, string, fileinput
def numwords(s):
list = string.split(s)
return len(list)

# Get the file name either from the command-line or the user
if len(sys.argv) != 2:
name = raw_input("Enter the file name: ")
else:
name = sys.argv[1]

inp = open(name,"r")
linelist = inp.readlines()
total, words,lines = 0, 0, 0

for i in range(len(linelist)):
line = linelist
tempwords = line.split()
for k in tempwords:
if keyword.iskeyword(k):
total = total + numwords(k)
j = i + 1
print" The word * %s * belongs in line number: %d" % (k,
j)

print "Total keywords in this file are: %d" %(total)


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


Oops, that over-counts, I forgot to put a continue in. Also, keeping a
cache of the split line will probably be faster.

for i,line in enumerate(linelist):
line = line.split()
for k in line:
if keyword.iskeyword(k):
total += line.count(k)
print "The word '%s' belongs in line num: %d" % (k, i+1)
continue

print "Total keyords are: %d" % total


James
 
J

James Stroud

James said:
James said:
Andy said:
Hi, the file below will print all the keywords in a file and also the
line # of the keyword. What I couldn't figure out is to count those
keywords per line. For example - "Line #1 has 3 keywords"

Can I do like -

total[j] = total[j] + numwords(k)
"Line number %d has %d keywords" % (j, total[j])

Seems sort of "illegal" in Python?



-------------------------------------------------
import keyword, sys, string, fileinput
def numwords(s):
list = string.split(s)
return len(list)

# Get the file name either from the command-line or the user
if len(sys.argv) != 2:
name = raw_input("Enter the file name: ")
else:
name = sys.argv[1]

inp = open(name,"r")
linelist = inp.readlines()
total, words,lines = 0, 0, 0

for i in range(len(linelist)):
line = linelist
tempwords = line.split()
for k in tempwords:
if keyword.iskeyword(k):
total = total + numwords(k)
j = i + 1
print" The word * %s * belongs in line number: %d" % (k,
j)

print "Total keywords in this file are: %d" %(total)


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


Oops, that over-counts, I forgot to put a continue in. Also, keeping a
cache of the split line will probably be faster.

for i,line in enumerate(linelist):
line = line.split()
for k in line:
if keyword.iskeyword(k):
total += line.count(k)
print "The word '%s' belongs in line num: %d" % (k, i+1)
continue

print "Total keyords are: %d" % total


James


I should really wait until I've had some coffee. Not continue, but break!


for i,line in enumerate(linelist):
line = line.split()
for k in line:
if keyword.iskeyword(k):
total += line.count(k)
print "The word '%s' belongs in line num: %d" % (k, i+1)
break

print "Total keyords are: %d" % total
 
R

rockmode

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"

;)
 
C

castironpi

Hi, the file below will print all the keywords in a file and also the
line # of the keyword. What I couldn't figure out is to count those
keywords per line. For example - "Line #1 has 3 keywords"

Can I do like -

total[j] = total[j] + numwords(k)
"Line number %d has %d keywords" % (j, total[j])

Seems sort of "illegal" in Python?

-------------------------------------------------
import keyword, sys, string, fileinput
def numwords(s):
list = string.split(s)
return len(list)

# Get the file name either from the command-line or the user
if len(sys.argv) != 2:
name = raw_input("Enter the file name: ")
else:
name = sys.argv[1]

inp = open(name,"r")
linelist = inp.readlines()
total, words,lines = 0, 0, 0

for i in range(len(linelist)):
line = linelist
tempwords = line.split()
for k in tempwords:
if keyword.iskeyword(k):
total = total + numwords(k)
j = i + 1
print" The word * %s * belongs in line number: %d" % (k,
j)

print "Total keywords in this file are: %d" %(total)

tempwords = line.split()
for k in tempwords: linec = 0
if keyword.iskeyword(k):
total = total + numwords(k)
j = i + 1 linec += 1
print" The word * %s * belongs in line number: %d" % (k,
j)
print "%i characters in line" % linec

And less readably,
tempwords = line.split()
for k in tempwords: linec = j
if keyword.iskeyword(k):
total = total + numwords(k)
j = i + 1
print" The word * %s * belongs in line number: %d" % (k,
j)
print "%i characters in line" % ( j - linec )
 
J

James Stroud

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"

;)


I think it would be obvious how to write this:


for i,line in enumerate(linelist):
line = line.split()
for k in line:
if keyword.iskeyword(k):
c = line.count(k)
total += line.count(k)
print "Line #%d has %d keywords." % (i+1, c)
break

print "Total keyords are: %d" % total
 
A

Andy

I pretty doubt about this - "c = line.count(k)" I might wanna recheck
on that.
-------------------------------------
I think it would be obvious how to write this:

for i,line in enumerate(linelist):
line = line.split()
for k in line:
if keyword.iskeyword(k):
c = line.count(k)
total += line.count(k)
print "Line #%d has %d keywords." % (i+1, c)
break

print "Total keyords are: %d" % total
 
A

Andy

James -

I pretty doubt about this - "c = line.count(k)" You might wanna
recheck on that.
------------------------------------------------
I think it would be obvious how to write this:

for i,line in enumerate(linelist):
line = line.split()
for k in line:
if keyword.iskeyword(k):
c = line.count(k)
total += line.count(k)
print "Line #%d has %d keywords." % (i+1, c)
break

print "Total keyords are: %d" % total
 
J

John Machin

I think it would be obvious how to write this:


for i,line in enumerate(linelist):
line = line.split()
for k in line:
if keyword.iskeyword(k):
c = line.count(k)
total += line.count(k)
print "Line #%d has %d keywords." % (i+1, c)
break

print "Total keyords are: %d" % total

I would have thought so too. But the above is ... let's just say it's
not quite right. If there are 3 different keywords (as in the OP's
example), the above code prints 3 times for the same line.

Here's a straight-forward natural way to do it:
total = 0
for i, line in enumerate(linelist):
c = 0
line = line.split()
for k in line:
if keyword.iskeyword(k):
c += 1
# Alternatively, replace above 5 lines by
# c = sum(keyword.iskeyword(k) for k in line.split())
# or the equivalent using map(), depending on taste etc :)
total += c
print "Line #%d has %d keywords." % (i+1, c)
print "Total number of keywords is", total

======

Perhaps someone should point out to the OP that using str.split as a
tokeniser is somewhat deficient:
1. comments and string literals could make the counts somewhat unreliable:
"# if not use mung(), will break while frobotzing later in code"
2. "else:"
3. "if not(0 <= n < maxn):"

HTH,
John
 
S

Steve Holden

James said:
James said:
James Stroud wrote:
[finally ...]

I should really wait until I've had some coffee. Not continue, but break!


for i,line in enumerate(linelist):
line = line.split()
for k in line:
if keyword.iskeyword(k):
total += line.count(k)
print "The word '%s' belongs in line num: %d" % (k, i+1)
break

print "Total keyords are: %d" % total

James is actually trying to convince you of the merits of test-driven
development by cleverly showing you the disadvantages of not using it.

Clever approach to advocacy, James. Did you get your coffee yet?

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top