类或对象,而不是Python 2中的字典 [英] class or object instead of dictionaries in Python 2

查看:185
本文介绍了类或对象,而不是Python 2中的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常使用嵌套字典,但我想转到类和对象。我有一份家庭工作成绩列表,如下所示:

  assignment_name assignment_subject student_name due_date grade grade_pct rank 

我想提出一个拥有所有学生的对象,每个学生都拥有所有的作业详细信息。我的字典看起来像:

 家庭作业['student_name'] ['assignment_name'] = {'assignment_subject':' ,'due_date':'11 / 12/13','grade':15,'grade_pct':15 / 20.0,'rank':'B'} 

应该是一个类中的类吗?

  class Homeworks对象):
def _init_(self):
self.students

类学生(对象):
def _init_(self,student_name):
self.student_name = student_name

def add_assignment(line):
student.assignment_name = line [0]
student.assignment_subject = line [1]
student.grade = line [4]

我知道这是不对的。我不知道我是否在正确的轨道上。我需要能够访问一个学生的成绩。 (在作业中的作业['Robbie Michaels']。键():打印家庭作业['Robbie Michaels'] [作业] ['grade'] )我也喜欢使用如果assignment_subject =='math'等访问它们。

解决方案

  class student:
def __init __(self,student_name):
self.name = student_name

class homework:

def __init __(self,student_name,assignment_name,assignment_subject,grade):
self.student = student(student_name)
self.assignment_name = assignment_name
self.assignment_subject = assignment_subject
self.grade = grade

@classmethod
def new_hw(cls,line):
return cls(line [0],line [1],line [2],line [3])

@classmethod
def get_all_grades(cls,student_name,homework_list):
return [x.grade for x in homework_list如果student_name是x.student.name]

l ines = [[abc,a,b,A],[abc,c,d,B],[ebc,c ,B]]
hw_list = [homework.new_hw(line)for line in lines]
print homework.get_all_grades(abc,hw_list)

您需要了解的是如何设计面向对象?
上面的代码可能不是最好的设计,因此,尝试这个来学习从哪里开始。 如何在Python中设计类?


I normally use nested dictionaries, but I'd like to move into classes and objects. I have a list of home work grades that look like:

assignment_name assignment_subject student_name due_date grade grade_pct rank

I'd like to make an object that holds all the students and each students holds all of their assignments details. My dictionary would look like:

homeworks['student_name']['assignment_name'] = {'assignment_subject': 'math', 'due_date': '11/12/13', 'grade': 15, 'grade_pct': 15/20.0, 'rank': 'B'}

Should this be a class within a class?

class Homeworks(object):
    def _init_(self):
        self.students

    class student(object):
        def _init_(self,student_name):
             self.student_name = student_name

        def add_assignment(line):
             student.assignment_name = line[0]
             student.assignment_subject = line[1]
             student.grade = line[4]

I know this isn't right. I don't know if I am on the right track or not. I do need to be able to access the grades for one student. (for assignment in Homeworks['Robbie Michaels'].keys(): print Homeworks['Robbie Michaels'][assignment]['grade']) I also like to access them using if assignment_subject == 'math' etc.

解决方案

class student:
    def __init__(self, student_name):
        self.name = student_name

class homework:

    def __init__(self,student_name,assignment_name,assignment_subject,grade):
        self.student = student(student_name)
        self.assignment_name = assignment_name
        self.assignment_subject = assignment_subject
        self.grade = grade

    @classmethod
    def new_hw(cls, line):
        return cls(line[0],line[1],line[2],line[3])

    @classmethod
    def get_all_grades(cls, student_name, homework_list):
        return [ x.grade for x in homework_list if student_name is x.student.name]

lines = [["abc","a","b","A"],["abc","c","d","B"],["ebc","c","d","B"]]
hw_list = [homework.new_hw(line) for line in lines]
print homework.get_all_grades("abc",hw_list)

What you need to understand is "How to design Object-Orientedly?". The above code, may not be the best of designs, hence, try this to learn where to start. How do I design a class in Python?

这篇关于类或对象,而不是Python 2中的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆