如何计算数字出现在文件中每个数字开头的次数? (蟒蛇)

时间:2014-02-19 22:46:25

标签: python

我试图计算1,2,3,...,9出现在文件中每个数字开头的次数。这就是我的代码:

DECIMAL_NUM='123456789'

def main():
    #get the file name from the user
    file_name=str(input("Enter a file name: "))

    #open the file to read
    input_file= open(str(file_name),'r')
    #reads the first line of the file
    line=input_file.readline().strip()

    one=0
    two=0
    three=0
    four=0
    five=0
    six=0
    seven=0
    eight=0
    nine=0
    i=0

    while line!="":

        if line[0]==DECIMAL_NUM[0]:
            one+=1            
        elif line[0]==DECIMAL_NUM[1]:
            two+=1
        elif line[0]==DECIMAL_NUM[2]:
            three+=1
        elif line[0]==DECIMAL_NUM[3]:
            four+=1
        elif line[0]==DECIMAL_NUM[4]:
            five+=1
        elif line[0]==DECIMAL_NUM[5]:
            six+=1
        elif line[0]==DECIMAL_NUM[6]:
            seven+=1
        elif line[0]==DECIMAL_NUM[7]:
            eight+=1
        elif line[0]==DECIMAL_NUM[8]:
            nine+=1           
        line=input_file.readline().strip()
        i+=1
    input_file.close()
    print(one)
    print(two)    
main()

我还在计算文件中有多少个数字,以便我可以计算每个数字的出现百分比。我认为我的代码有点罗嗦,可能有更好的方法。输入文件包含以下数字:

1292

1076

188040

1579

3510

2597

3783

64690

出于某种原因,我得到1次出现的次数为1次,应该是5次。有人可以给我一些指示吗?感谢

2 个答案:

答案 0 :(得分:1)

以下是处理此任务的一种方法:

# Get non-empty lines from input file:
relevant_lines = [line for line in open(file_name).readlines() if line.strip()]
# Count them:
num_lines = len(relevant_lines)

import defaultdict
# If a key does not exist in a defaultdict when adding a value for it,
# it will be added with a default value for the given data type
# (0 in case of int):
d = defaultdict(int)

# Iterate through lines; get first character of line
# and increment counter for this character by one in defaultdict:
for line in relevant_lines:
    d[line[0]] += 1

# Print results:
for key, value in d.items():
    print(k + ' appears ' + value + ' times in file.')

如果您不允许使用dict,请按以下步骤修改代码:

DECIMAL_NUM='123456789'

def main():
    # Get file name from user
    file_name = input("Enter a file name: ")

    # Open the file to read, and get a list of all lines:
    lines = open(file_name, 'r').readlines()

    one = 0
    two = 0
    three = 0
    four = 0
    five = 0
    six = 0
    seven = 0
    eight = 0
    nine = 0

    for line in lines:

        if line.strip(): # Check if line is not empty

            if line[0] == DECIMAL_NUM[0]:
                one += 1            
            elif line[0] == DECIMAL_NUM[1]:
                two += 1
            elif line[0] == DECIMAL_NUM[2]:
                three += 1
            elif line[0] == DECIMAL_NUM[3]:
                four += 1
            elif line[0] == DECIMAL_NUM[4]:
                five += 1
            elif line[0] == DECIMAL_NUM[5]:
                six += 1
            elif line[0] == DECIMAL_NUM[6]:
                seven += 1
            elif line[0] == DECIMAL_NUM[7]:
                eight += 1
            elif line[0] == DECIMAL_NUM[8]:
                nine += 1

    print(one)
    print(two)

main()

答案 1 :(得分:0)

你的代码很好。这是你的数据文件,它给你带来了问题。删除空白行,您的程序应该能够为您提供正确的结果。

1292
1076
188040
1579
3510
2597
3783
64690

处理完第一行后,将读取下一行。但这是一个空白行,你的while循环结束了。