简体   繁体   English

列表中字典中的Python列表-引用时不带整数(字符串?)

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

This question problem is from code academy. 这个问题的问题来自代码学院。 I have come up with a solution although not quite satisfied, as I think I am programmatically cheating to make code work. 我提出了一个解决方案,尽管并不十分满意,因为我认为我在编程上作弊以使代码正常工作。 Are there other ways to solve this. 还有其他方法可以解决此问题。 I have seen harder code. 我看过更难的代码。 This one is fairly straight forward. 这是相当简单的。

Three dictionaries on students that have four keys. 关于具有四个键的学生的三本词典。 Three of the keys are lists. 其中三个键是列表。 function to calculate average function to calculate letter grade function to calculate average on each kid 计算平均值的函数计算字母等级的函数计算每个孩子的平均值的函数

Okay thing is, calling the average on each kid function to calculate the average works fine as do all the functions. 好的,就是调用每个kid函数的平均值来计算平均值,就像所有函数一样。

However, if a new list is create to include all 3 students --- that makes student list, with 3 list items. 但是,如果创建了一个包含所有3个学生的新列表---这将使学生列表包含3个列表项。 Each item has a dictionary, with four keys. 每个项目都有一个字典,带有四个键。 Three of the four keys are lists. 四个键中的三个是列表。 Phew!!!

I have used a for loop with a counter and an increment in the getClassAverage function to calculate average. 我使用了带计数器的for循环和getClassAverage函数中的增量来计算平均值。

Questions: 问题:

  • Is there another way to calculate average without a for loop ie just within the function as just using "who" passed argument makes it have the students (data) and does not reference well with the list. 还有另一种没有for循环的方法来计算平均值,即仅在函数内,因为仅使用“ who”传递的参数使其具有学生(数据),并且不能很好地引用列表。 Gives indexing with string, not integer" error? 使用字符串而不是整数建立索引”错误?

  • With list inside key, inside list. 带有内部列表键,内部列表。 How will I print a student name from the getClassAverage function? 如何从getClassAverage函数打印学生姓名?

Thanks! 谢谢!

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))

To estimate average, numpy has a function that you can use. 要估算平均值,numpy具有可以使用的功能。

from numpy import average

In getClassAverage function you are passing a list with the names of the students. 在getClassAverage函数中,您要传递一个包含学生姓名的列表。 So if you do: 因此,如果您这样做:

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

It will print the name of the student as you estimate his average grade to add to the class average. 当您估计学生的平均成绩并将其添加到班级平均值时,它将打印学生的姓名。

Hope that helps. 希望能有所帮助。

Using your code and functions you already have, try this: 使用您已有的代码和功能,请尝试以下操作:

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

Your code is fine. 您的代码很好。 I will just post how I would have possibly implemented this to give you another point of few, and not to say that my version be better or nicer: 我将发布如何实现这一点,以使您再说几句话,而不是说我的版本更好或更漂亮:

#! /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, I know it, I have read it, I choose to ignore it.) (Inb4 PEP8,我知道,我已经阅读过,选择忽略它。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM