Basic01x
Basic01x

Reputation: 1

How to count number in list?

I have list of numbers from 1 to 20. Random 10 numbers. I need to print how much does number 1, 2, 3, 4 etc.. until 20 appears in the list.

The problem is that when it counts it will count 11, 12, 13, 14 etc. as 1. How can I set it when it comes to 11 it will NOT count it to 1, and if it comes to other numbers ex: 12,13,14, it will NOT count it to 1 or 2,3,4.

Thank you for answers!

count = 0
f = open("numbers.txt")
for line in f:
    count += line.count("1")


print (count)
f.close()

Upvotes: 0

Views: 293

Answers (5)

Basic01x
Basic01x

Reputation: 1

import csv 

import re

count = 0
count2 = 0
num1 = '1'
num2 = '2'




with open('numbers.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        print(row)
        for line in row:
            if line == num1:
                count += 1
            elif line == num2:
                count2 += 1

print("Num1: "), count
print("Num2: "), count2

Upvotes: 0

Jack Casey
Jack Casey

Reputation: 1836

The description is a bit ambiguous, so I'm taking a bit of a guess what you are looking for. Are you looking to print the numbers 1 to 20? If so, this is how I would do it:

Printing to console

for i in range(20):
  print(i + 1)

Writing to file

with open("numbers.txt", 'w') as f:
    for i in range(20):
        f.write("{}\n".format(str(i + 1)))

If this is not what you are looking for, can you provide a bit more detail? Possibly expected input / output?

Edit If you're looking to count the instances in the file:

searchNum = 10
count = 0

with open("num.txt", "r") as f:
    for line in f:
        if int(line.strip()) == searchNum:
            count += 1

strip() is used to remove any whitespace on the line

edit edit Here is another style using list comprehensions:

searchNum = 10

with open("num.txt", "r") as f:
    list = [int(line.strip()) for line in f.readlines()]
    count = list.count(searchNum)

Upvotes: 2

sahasrara62
sahasrara62

Reputation: 11228

f = open(r"result.txt").read()
count = f.count('1')
print(count)

output

12

Upvotes: 0

HK boy
HK boy

Reputation: 1406

You are now counting digit characters in every line.

for line in f:  # line is string
    count += line.count("1")  # oh no! digit characters are counted!

To solve the problem, you must restore the real list of number.

To get the list of "number", you can split the content by whitespaces. The "number"s are now strings.

number_in_str = f.read().split()

Next, convert it to number, then count.

numbers_list = [int(u) for u in number_in_str]

count = numbers_list.count(1)

Upvotes: 1

nimishxotwod
nimishxotwod

Reputation: 335

You can do this by converting the string into an integer for each line. For one number in each

for line in f:
    count += 1 if int(line) == 1

If the file has multiple integers per line :

for line in f:
    num_arr = line.split(' ')
    count += num_arr.count('1') #this works now since num_arr is a list of string                                                                              

Upvotes: 2

Related Questions