计算子列表中 5 个值中前 4 个 int 值的平均值。 (2个值相同)

Calculating average of top 4 int values out of 5 values in a sub-list. (2 values are same)

给定以下列表

student_marks = [['Name',  ['A','B','C','D','E']],
                 ['Ankit',  [41, 34, 45, 55, 63]],
                 ['Aravind',[42, 23, 34, 44, 53]],
                 ['Lakshay',[32, 23, 13, 54, 67]],
                 ['Gyan',   [23, 82, 23, 63, 34]],
                 ['Pranav', [21, 23, 25, 56, 56]]
                ]

如果考虑学生的前 4 个科目,我试图找出谁的分数百分比最高(前 4 个分数的平均值)。我们需要比较每个学生前 4 名的平均分,找出谁的百分比最高。

这是我所做的:

best_four_marks = []


for i in student_marks[1:]:
    student_name = i[0]
    count_1 = 0
    summ_1 = 0
    for j in i[1]:
        if j > min(i[1]):
            count_1 += 1
            summ_1 += j
    avg = summ_1/count_1
    best_four_marks.append([avg,student_name])

这里的问题是其中一名学生,即 Gyan 在科目 A 和科目 C 中有 2 个相同的值,即 23 分。我已指示编译器忽略最差分数来计算前 4 名每个学生的科目百分比。在 Gyan 的迭代中,它完全忽略了 5 个主题中的 2 个最低值。我想包括其中之一,因为我们需要前 4 名主题的百分比。

期待听到您的反馈!谢谢

您可以通过从总和中减去最低分来解决问题。

best_four_marks = []

for i in student_marks[1:]:
    student_name = i[0]
    summ_1 = sum(i[1]) - min(i[1])
    count_1 = len(i[1])-1
    avg = summ_1/count_1
    best_four_marks.append([avg,student_name])
student_marks = [
    ['Name',  ['A','B','C','D','E']],
    ['Ankit',  [41, 34, 45, 55, 63]],
    ['Aravind',[42, 23, 34, 44, 53]],
    ['Lakshay',[32, 23, 13, 54, 67]],
    ['Gyan',   [23, 82, 23, 63, 34]],
    ['Pranav', [21, 23, 25, 56, 56]]
]
avg_marks = {}
for marks in student_marks[1:]: # ignore first entry
    student = marks[0]
    top_4_marks = sorted(marks[1], reverse=True)[:-1]
    total_of_top_4_marks = sum(top_4_marks)
    avg_marks[student] = total_of_top_4_marks/4
avg_marks

输出

{'Ankit': 51.0,
 'Aravind': 43.25,
 'Lakshay': 44.0,
 'Gyan': 50.5,
 'Pranav': 40.0}

可能不是最干净的方法,但我会用 sorted()

像这样...

for student in student_marks[1:]:
    student_name = student[0]
    grades_count = len(student[1]) - 1
    grades = sorted(student[1][1:])
    student_average = 0
    for grade in grades:
        student_average = student_average + grade
    student_average = student_average / grades_count
    best_four_marks.append([student_name, student_average])

我会避免第二个循环,只需使用

student_average = sum(grades) / grades_count

也许这个解决方案可读性强且易于更改

student_marks = [['Name', ['A', 'B', 'C', 'D', 'E']],
                 ['Ankit', [41, 34, 45, 55, 63]],
                 ['Aravind', [42, 23, 34, 44, 53]],
                 ['Lakshay', [32, 23, 13, 54, 67]],
                 ['Gyan', [23, 82, 23, 63, 34]],
                 ['Pranav', [21, 23, 25, 56, 56]]
                 ]

output = []
for student in student_marks:
    student_name = student[0]
    list_marks = student[1]
    
    # Check if string exists in the mark list
    if any(isinstance(element, str) for element in list_marks):
        continue

    list_marks = sorted(list_marks, reverse=True)[:-1]  # remove the lowest mark
    average_mark = sum(list_marks) / len(list_marks)
    output.append([student_name, average_mark])

因为你想从 5 中得到 4 的平均值,这对每个学生来说都是更好的代码

def getBest4Average(listScore):
    return (sum(listScore) - min(listScore))/4

getBest4Average([20, 30, 10, 50, 60])

结果:

40.0

如果你想要一个从 n

中计算 (n-1) 的版本
def getBestAverage(listScore):
    size = len(listScore)
    return (sum(listScore) - min(listScore))/(size - 1)

如果你想要 n 最好的 >n 元素列表,如果 n 是小这个方法会有更好的性能

def popmax(l):
    m = max(l)
    l.remove(m)
    return m

def GetNBestAverage(listScore, n):
    best = []
    __listScore = list(listScore) # i do not want to hurt the original list
    for i in range(0, n):
        best.append(popmax(__listScore))
    return sum(best) / len(best)