[英]Count Number of times each grade appears in a file
I am trying to print out the number of times each grade appears in a file. 我正在尝试打印每个年级出现在文件中的次数。 score = [89, 45, 67, 78, 98, 0] * This can change for different files, can gain more numbers etc.
分数= [89、45、67、78、98、0] *可以针对不同的文件进行更改,可以获取更多的数字,等等。
low and high scores have already been figured out. 低分数和高分数已经被弄清楚了。 I only need to display the scores between them.
我只需要显示它们之间的分数。 What am I missing?
我想念什么? It isnt counting the numbers, just printing out this...
它不算数字,只是打印出来...
38: 0
39: 0
40: 0
41: 0
42: 0
43: 0
44: 0
45: 0
46: 0
47: 0
48: 0
49: 0
50: 0
51: 0
52: 0
53: 0
54: 0
55: 0
56: 0
57: 0
58: 0
59: 0
60: 0
61: 0
62: 0
63: 0
64: 0
65: 0
66: 0
67: 0
68: 0
69: 0
70: 0
71: 0
72: 0
73: 0
74: 0
75: 0
76: 0
77: 0
78: 0
79: 0
80: 0
81: 0
82: 0
83: 0
84: 0
85: 0
86: 0
87: 0
88: 0
89: 0
90: 0
91: 0
92: 0
93: 0
Any the code is: 任何代码是:
def histogram(score, low, high):
e = int(low)
o = int(high)
for i in range(e, o):
print(str(i)+": "+str(score.count(i)))
print()
Not sure if this is what you are looking for. 不知道这是您在寻找什么。 "Frequency of the list"
“列表的频率”
import collections
score = [89, 45, 67, 78, 98, 0]
counter=collections.Counter(score)
print(counter)
# Counter({0: 1, 98: 1, 67: 1, 89: 1, 45: 1, 78: 1})
print(counter.most_common())
# [(0, 1), (98, 1), (67, 1), (89, 1), (45, 1), (78, 1)]
Your scores array is a list of strings. 您的成绩数组是一个字符串列表。 Either map it to a list of integers:
要么将其映射到整数列表:
score = map(int, score)
Or count the string value at each step: 或在每个步骤中计算字符串值:
print(str(i)+": "+str(score.count(str(i))))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.