string.count issue (simple)

Michael Peuser mpeuser at web.de
Fri Aug 29 10:28:11 EDT 2003


Hello Halfdan,

it is a pity that you did not copy&paste the code in question.
What you posted cannot work - count has no keyword parameters!

On the other hand the use of find AND count is quite redundant...

This snippet works fine:

  count = 0
  for eachLine in allLines:
      count += eachLine.count(word)

Kindly
Michael P

"Halfdan Holger Knudsen" <s011362 at bohr.gbar.dtu.dk> schrieb im Newsbeitrag
news:mailman.1062163536.18292.python-list at python.org...
> ok first off: Has the string.count function been replaced in python 2.2
> (as compared to 1.5)?m And second:
>
> I need this in relation to a stringsearch program
> assuming a text file has been loaded into allLines and word contains the
> string you're searching for I want to count all occurences of the search
> string the following should do it (but doesn't - please disregard any
> missing colons or such this has not been copy/pasted):
>
> count = 0
>
> for eachLine in allLines:
>     if eachLine.find(word) > -1:
>         count = count + eachLine.count(word, beg=0, end=len(eachLine)
>         print eachLine
> print count
>
> what am I missing - it only outputs the nubmer of lines (as if the count
> statement had been count = count + 1)
>
> thanks
> H
>






More information about the Python-list mailing list