Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8517953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:51:00+00:00 2026-06-11T05:51:00+00:00

I have a file called !input.txt containing multiple lines, each line is a random

  • 0

I have a file called !input.txt containing multiple lines, each line is a random integer from 0 to 10.
I want to write a program that reads the file and calculate how many times each integer (0-10) appears in the file. For example, if there are four “0”, two “3” and five “7” in my input file, the program will print out something like this:

Number of occurrences of 0: 4
Number of occurrences of 1: 0
Number of occurrences of 2: 0
Number of occurrences of 3: 2
Number of occurrences of 4: 0
Number of occurrences of 5: 0
Number of occurrences of 6: 0
Number of occurrences of 7: 5
Number of occurrences of 8: 0
Number of occurrences of 9: 0
Number of occurrences of 10: 0

Here is my code:

mylist = [0,1,2,3,4,5,6,7,8,9,10]
countlist = []
inFile = open("!input.txt", "r")
count = 0

for digit in mylist:
    for line in inFile:
        if digit == int(line):
            count = count + 1
    countlist.append(count)
    count = 0

#Print out the result#
for i in range(11):
    print("Number of occurrences of {0}: {1}".format(i, countlist[i]))

The result comes out like this:

Number of occurrences of 0: 4
Number of occurrences of 1: 0
Number of occurrences of 2: 0
Number of occurrences of 3: 0
Number of occurrences of 4: 0
Number of occurrences of 5: 0
Number of occurrences of 6: 0
Number of occurrences of 7: 0
Number of occurrences of 8: 0
Number of occurrences of 9: 0
Number of occurrences of 10: 0

I guess there’s something wrong with my nested for loop, but I couldn’t figure out what it is. Please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:51:02+00:00Added an answer on June 11, 2026 at 5:51 am

    One problem with your loop is that you can only loop over inFile once, because it’s an open file object. After you’ve read through it on the first pass, you’re at the end of the file, and there are no more lines to read. So the variant

    inFile = open("input.txt", "r").readlines()
    for digit in mylist:
        count = 0
        for line in inFile:
            if digit == int(line):
                count = count + 1
        countlist.append(count)
    

    should work. However, this requires both reading all the lines into memory, and doing a loop for each digit. More efficient would be to preallocate the space and do one pass:

    mylist = [0,1,2,3,4,5,6,7,8,9,10]
    countlist = [0]*len(mylist)
    inFile = open("input.txt", "r")
    for line in inFile:
        digit = int(line)
        countlist[digit] += 1
    

    Better still, use a defaultdict or Counter:

    from collections import Counter
    inFile = open("input.txt", "r")
    count = Counter()
    for line in inFile:
        count[int(line)] += 1
    

    leading up to the semi-magical:

    with open("input.txt") as fp:
        count = Counter(int(line) for line in fp)
    

    PS: don’t forget to close your file objects. I’m too lazy to go back and do it myself, but you should. :^)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have text file. there around 1000 lines in it, each line contains a
I have a file called mail.txt in which lines are printed like below ,i
I have a file (called print_1012720.txt ) that looks like the text shown below.
I have a file called file1.txt: dir1 dir2 dir3 ... I wanted to use
I have a list of files [input1.txt,input2.txt,input3.txt] I want to create a new file
I have a file in the res/raw folder called book1tabs.txt, but in general I
I have a file called middle.config that is deployed in the same directory as
I have a file called video_2.m4v on my computer, which is being used by
I have a file called header.php that I am including on every page on
I have a file called header.html and it is included by base.html . In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.