-1

In Codio, there is a challenge to take a file, search for the number of times a string appears in it, then print that number. I was able to get the result using some suggestions, but I am still unclear on a few things.

The main question is, at what point is the loop searching for the substring S? The count() syntax that I see everywhere involves using the string to be searched, followed by the dot operator, and then the function with the substring we want to find as the parameter. It would look something like: P.count(S)

What confuses me is that the function is using line in place of P. So does this mean the function is searching line for the substring? And if so, how does that work if line is simply the counter variable for the for loop? I just want to have a clearer understanding of how this function is working in this context to get me the correct count of times that substring S appears in file P.

import sys
P= sys.argv[1]
S= sys.argv[2]

# Your code goes here

f = open(P, 'r')
c = 0

for line in f.readlines():
  c += line.count(S)

print(c)
Prune
  • 76,765
  • 14
  • 60
  • 81
Toph Hughes
  • 101
  • 1
  • 10
  • If `S` is your substring you are trying to count the occurrances of in the file `P`, then when you call `line.count(S)` it is counting how many times `S` occurs in the string `line`. `line` is not a counter for the for loop, it is each individual line string from the list returned by `f.readlines()` that the for loop iterates through. – Karl Oct 09 '18 at 16:07

2 Answers2

1

does this mean the function is searching "line" for the substring

Yes, that's exactly what it means. And the value of line changes in every loop iteration.

And if so, how does that work if "line" is simply the counter variable for the "for" loop

It's not. Python for loops don't have counters. line is the actual line of text.

for letter in ['A', 'B', 'C']:
    print(letter)

prints

A
B
C
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • Re "Python `for` loops don't have counters": you can achieve that using `for index, item in enumerate(collection)` to get both index/count and the item at the same time. – zxxc Oct 09 '18 at 16:21
  • I anticipated that comment. Yes, you can loop over a generator for `(number, value)` tuples. That loop *still* does not have a counter. Through what can be called a happy accident, the `number` correlates with the item index, but it's not the loop counter. – Tomalak Oct 09 '18 at 16:38
0

Let's dissect the loop:

for line in f.readlines():
  c += line.count(S)

f is a file descriptor of your open file.

readlines is a generator, a function sort of thing that returns the lines of the file. If you think of it as a list of strings, each of which is a line of the file, you'll be close enough to understand the loop operation.

Thus, the statement for line in f.readlines(): iterates the variable line through the file contents; on each loop iteration, line will be the appropriate string value, the next line of the file.

Therefore, line.count(S) returns the quantity of times the target string S appears in that line of the file. The increment c += adds that to your counter.

Does that make things clear enough?


BTW, please learn to use descriptive variable names. One-letter names with mixed upper- and lower-case are a bad habit in the long run.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • Regarding the variable names, they are not all of my making. Some of them are pre-loaded by Codio and whatever the course I am taking had set up for it. Thanks for the tip though. – Toph Hughes Oct 09 '18 at 17:24
  • Glad to hear it. As a general policy, I recommend avoiding "educational" sites that exhibit substandard practices. There are a lot of "coding challenge" sites. – Prune Oct 09 '18 at 17:26
  • Yeah, I unfortunately am a coding noobie. And as such, I am in a college course for beginner level Python. This Codio site is what they use for reading material as well as learning objectives and challenges. I will probably try to pursue some external stuff to reinforce my understanding of these languages. – Toph Hughes Oct 09 '18 at 18:03
  • Also, the problem I was running into was my confusion between this code I posted, and previous code that involved using 'for' loops with the keywords 'in' and 'range'.As an example: for x in range (0, 6): . My book that is teaching me this uses the word "counter" to describe the variable 'x' in this kind of example. I don't think it applies the same to every instance of a 'for' loop though. – Toph Hughes Oct 09 '18 at 18:05