我如何将每个类别中每个年级的分数作为一组分数进行计数

时间:2019-02-11 04:26:30

标签: python python-3.x

我得到了一组标记

midtermMarks = [48.0, 64.25, 71.80, 82.0, 53.45, 59.75, 62.80, 26.5,
55.0, 67.5, 70.25, 52.45, 66.25, 94.0, 65.5, 34.5, 69.25, 52.0]

我必须找到多少个A。假设A为100-80,B为79-70,C为69-60,这是B和C的集合。

1 个答案:

答案 0 :(得分:3)

首先,您需要创建一个具有键 '60 -70' '70 -80' '80 -100'的字典每个值都为0。使用 for 循环获取每个标记,并使用 if 语句检查每个条件,如果条件满足,则将1添加到各个键中。

  

我为你做了。希望它能起作用。

[CODE]

import pprint     # This module prints dictionary in a pretty way

marks = [48.0, 64.25, 71.80, 82.0, 53.45, 59.75, 62.80, 26.5,
     55.0, 67.5, 70.25, 52.45, 66.25, 94.0, 65.5, 34.5, 69.25, 52.0]

counting = {'Number of A\'s': 0, 'Number of B\'s': 0, 'Number of C\'s': 0}  # Dictionary to count the number in given range

for mark in marks:  # Getting each marks from marks list
    if mark >= 80:  # Checking if mark is more than 80
        counting['Number of A\'s'] += 1  # Adding 1 if there is repetition to dictionary

    elif mark >= 70 and mark < 80:  # Checking if mark is more than and equals to 70 but less than 80
        counting['Number of B\'s'] += 1  # Adding 1 if there is repetition to dictionary

    elif mark >= 60 and mark < 70:  # Checking if mark is more than and equals to 60 but less than 70
        counting['Number of C\'s'] += 1  # Adding 1 if there is repetition to dictionary

pprint.pprint(counting)    # Printing the output

[输出]

{"Number of A's": 2, "Number of B's": 2, "Number of C's": 6}