Home > database >  Exiting "for" loop too soon yielding inaccurate count
Exiting "for" loop too soon yielding inaccurate count

Time:06-22

In my program, I am iterating through lines in a .txt file. I am adding these lines to a dictionary depending on if they begin with a text or numeric value. Between instances of numeric lines, I want to count the number of lines with strings that occur. I also add both the numeric line's content and string line count to a dictionary.

My file looks something like this: 6/10/21 string string string 6/11/21

However, I exit the loop too early and I'm not sure why. For example, my program would only count two lines of strings instead of three. Here's some of my code:

    count = 0
    for i in myfile:
      if i[0].isdigit():
        s = i.strip()
        my_dict["Numeric"].append(s)
      if i[0].isdigit()==False:
        count = count   1
        next_ln = next(myfile)
        if next_ln[0].isdigit():
          print(next_ln)
          my_dict["String Count"].append(count)
          count = 0

In another version of my code, I have the final if-statement aligned with the others but it doesn't change anything. Sorry if this has been asked before or if this is a really obvious problem, but I couldn't find anything that helped resolve my issue.

CodePudding user response:

I'm assuming my_dict["String Count"] is the list of count of string lines, and my_dict["Numeric"] the list of numeric lines.

The goal here is to avoid the use of next() within the for-loop, as that is messing up the functioning of the for-loop, and hence giving you undesired results.

Here's my solution:

for i in myfile:
    if i[0].isdigit():
        s = i.strip()
        my_dict["Numeric"].append(s)
        
        # initiating a new element 0 to the count list
        my_dict["String Count"].append(0)
    else:
        # incrementing the value of the last present number in the count list by 1
        my_dict["String Count"][-1]  = 1

Here I've also assumed that whatever doesn't start numerically, is a string line. You may make the necessary modifications to the else clause into an elif or any other condition you want.

The end result is that, every numeric line in my_dict["Numeric"] will have a corresponding number (at the same index) in the my_dict["String Count"] containing the number of string lines that comes after it.

  • Related