By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

Counting

P: n/a
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[i]
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)

Apr 29 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Andy wrote:
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[i]
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
Apr 29 '07 #2

P: n/a
James Stroud wrote:
Andy wrote:
>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[i]
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
Apr 29 '07 #3

P: n/a
James Stroud wrote:
James Stroud wrote:
>Andy wrote:
>>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[i]
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
Apr 29 '07 #4

P: n/a
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

Apr 29 '07 #5

P: n/a
On Apr 29, 2:11 pm, Andy <andy.rockf...@gmail.comwrote:
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[i]
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 )

Apr 29 '07 #6

P: n/a
ro******@gmail.com wrote:
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
Apr 29 '07 #7

P: n/a
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

Apr 30 '07 #8

P: n/a
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

>
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

Apr 30 '07 #9

P: n/a
On 30/04/2007 7:17 AM, James Stroud wrote:
ro******@gmail.com wrote:
>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
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
Apr 30 '07 #10

P: n/a
James Stroud wrote:
James Stroud wrote:
>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 ----------------

May 5 '07 #11

P: n/a
..o0

May 7 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.