[英]How to count the number of times a digit appears at the beginning of each number in a file? (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()
我还要计算文件中有多少个数字,以便可以计算每个数字出现的百分比。 我认为我的代码有点罗word,可能会有更好的方法来做到这一点。 输入文件具有以下数字:
1292
1076
188040
1579
3510
2597
3783
64690
由于某种原因,我得到1出现为1的次数,应该为5。有人可以给我一些指示吗? 谢谢
这是完成此任务的一种方法:
# 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()
您的代码很好。 是您的数据文件给您带来了麻烦。 删除空白行,您的程序应该给您正确的结果。
1292
1076
188040
1579
3510
2597
3783
64690
处理第一行后,将读取下一行。 但这是空白行,而while循环结束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.