簡體   English   中英

列表中字典中的Python列表-引用時不帶整數(字符串?)

[英]Python list in dictionary in list - referencing without integers (strings?)

這個問題的問題來自代碼學院。 我提出了一個解決方案,盡管並不十分滿意,因為我認為我在編程上作弊以使代碼正常工作。 還有其他方法可以解決此問題。 我看過更難的代碼。 這是相當簡單的。

關於具有四個鍵的學生的三本詞典。 其中三個鍵是列表。 計算平均值的函數計算字母等級的函數計算每個孩子的平均值的函數

好的,就是調用每個kid函數的平均值來計算平均值,就像所有函數一樣。

但是,如果創建了一個包含所有3個學生的新列表---這將使學生列表包含3個列表項。 每個項目都有一個字典,帶有四個鍵。 四個鍵中的三個是列表。

我使用了帶計數器的for循環和getClassAverage函數中的增量來計算平均值。

問題:

  • 還有另一種沒有for循環的方法來計算平均值,即僅在函數內,因為僅使用“ who”傳遞的參數使其具有學生(數據),並且不能很好地引用列表。 使用字符串而不是整數建立索引”錯誤?

  • 帶有內部列表鍵,內部列表。 如何從getClassAverage函數打印學生姓名?

謝謝!

Lloyd = {
    "name":"Lloyd",
    "homework": [90,97,75,92],
    "quizzes": [ 88,40,94],
    "tests": [ 75,90]
    }
Alice = {
    "name":"Alice",
    "homework": [100,92,98,100],
    "quizzes": [82,83,91],
    "tests": [89,97]
    }
Tyler = {
    "name":"Tyler",
    "homework": [0,87,75,22],
    "quizzes": [0,75,78],
    "tests": [100,100]
    }

def average(stuff):
    return sum(stuff)/len(stuff)

def getLetterGrade(score):
    score = round(score)
    if  score >= 90: return "A"
    elif  90 > score >= 80: return "B"
    elif  80 > score >= 70: return "C"
    elif  70 > score >= 60: return "D"
    elif  60 > score: return "F"

def getAverage(kid):
    bar = average
    return bar(kid["homework"])*.1 + bar(kid["quizzes"])*.3 + bar(kid["tests"])*.6

students = [Lloyd,Alice,Tyler]

def getClassAverage(who):
    class_ave = 0
    class_avetot = 0
    count = 0
    for x in who:
        class_avetot = class_avetot + getAverage(who[count])
        #print who[count]
        stu_score = getAverage(who[count])
        print stu_score
        print getLetterGrade(stu_score)
        count = count + 1
    class_ave = class_avetot / len(who)
    return class_ave

print str(getClassAverage(students))

要估算平均值,numpy具有可以使用的功能。

from numpy import average

在getClassAverage函數中,您要傳遞一個包含學生姓名的列表。 因此,如果您這樣做:

for x in who:
        print x  # This prints the name of the student
        class_avetot = class_avetot + getAverage(who[count])

當您估計學生的平均成績並將其添加到班級平均值時,它將打印學生的姓名。

希望能有所幫助。

使用您已有的代碼和功能,請嘗試以下操作:

def getClassAverage (who):
    return average ( [getAverage (kid) for kid in who] )

您的代碼很好。 我將發布如何實現這一點,以使您再說幾句話,而不是說我的版本更好或更漂亮:

#! /usr/bin/python3.2

def ave (x): return sum (x) / len (x)

def getLetterGrade (score):
    score = round (score)
    if score >= 90: return 'A'
    if score >= 80: return 'B'
    if score >= 70: return 'C'
    if score >= 60: return 'D'
    #Why no grade E?
    return 'F'

class Student:
    def __init__ (self, name, homework, quizzes, tests):
        self.name = name
        self.homework = homework
        self.quizzes = quizzes
        self.tests = tests

    @property
    def average (self):
        return ave (self.homework) * .1 + ave (self.quizzes) * .3 + ave (self.tests) * .6

    def printAverage (self):
        print ('{} has an average of {:.2f} ({})'.format (self.name, self.average, getLetterGrade (self.average) ) )

class Class:
    def __init__ (self, students):
        self.students = students

    @property
    def average (self):
        return ave ( [student.average for student in self.students] )

    def printStudents (self):
        for student in students:
            student.printAverage ()

    def printAverage (self):
        print ('The class has an average of {:.2f}'.format (self.average) )

students = [Student ('Lloyd', [90,97,75,92], [88,40,94], [75,90] ),
    Student ('Alice', [100,92,98,100], [82,83,91], [89,97] ),
    Student ('Tyler', [0,87,75,22], [0,75,78], [100,100] ) ]

myClass = Class (students)
myClass.printStudents ()
myClass.printAverage ()

(Inb4 PEP8,我知道,我已經閱讀過,選擇忽略它。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM