Python 如何使用嵌套*嵌套字典*

Python 如何使用嵌套*嵌套字典*,python,dictionary,nested,Python,Dictionary,Nested,因此,如果我能够嵌套字典,我的代码将看起来最整洁。 我的问题是: 嵌套字典是真的吗?(这可能吗?) 我如何设置它们 如何获取子字典键的值 下面是我正在尝试使用的一些代码。请告诉我正确的格式是什么,可能有什么错误。。。谢谢 Student.Alice = { Student.Alice.Math = { 'math1'= 100 'math2'= 98 'math3' = 89

因此,如果我能够嵌套字典,我的代码将看起来最整洁。 我的问题是:

  • 嵌套字典是真的吗?(这可能吗?)
  • 我如何设置它们
  • 如何获取子字典键的值
  • 下面是我正在尝试使用的一些代码。请告诉我正确的格式是什么,可能有什么错误。。。谢谢

    Student.Alice = {
                Student.Alice.Math = {
                    'math1'= 100
                    'math2'= 98
                    'math3' = 89
                    'math4' = 91
                    'math5' = 77
                    'math6' = 90
                    'math7' = 82
                    'math8' = 100
                    'math9' = 79
                    'math10' = 100
                    }
                Student.Alice.English = {
                    'english1' = 100
                    'english2' = 97
                    'english3' = 98
                    'english4' = 88
                    'english5' = 94
                    'english6' = 95
                    'english7' = 98
                    'english8' = 82
                    'english9' = 84
                    'english10' = 99
                    }
                Student.Alice.Science = {
                    'science1' = 78
                    'science2' = 89
                    'science3' = 88
                    'science4' = 92
                    'science5' = 92
                    'science6' = 91
                    'science7' = 93
                    'science8' = 88
                    'science9' = 99
                    'science10' = 87
                    }
                Student.Alice.French = {
                    'french1' = 100
                    'french2' = 100
                    'french3' = 99
                    'french4' = 104
                    'french5' = 103
                    'french6' = 97
                    'french7' = 94
                    'french8' = 93
                    'french9' = 75
                    'french10' = 93
                    }
                }
    
    1、2)是的,嵌套字典是可能的

    d = {'a': 3, 'b': 5}
    e = {'a': 4, 'b': 7}
    f = {'foo': d, 'bar': e}
    
    3) 您可以通过以下方式访问子字典元素:

    print f['bar']['a']
    
    哪个将输出
    4

    所以在你的例子中,你可以有一个名为“学生”的字典,每个学生都有一个科目字典,每个科目都有一个分数列表。差不多

    students = {
        'Alice': {
            'Maths': [1, 56, 23, 56],
            'Science': [23, 53, 43],
            ...
        },
        'Bob': {
            'Maths': [1, 56, 23, 56],
            'Science': [23, 53, 43],
            ...
        },
        ...
    }
    
    要获得Bob的第二个数学成绩,您可以使用学生['Bob']['Math'][1](不要忘记列表项从0开始索引)。

    1,2)是的,可以使用嵌套字典

    d = {'a': 3, 'b': 5}
    e = {'a': 4, 'b': 7}
    f = {'foo': d, 'bar': e}
    
    3) 您可以通过以下方式访问子字典元素:

    print f['bar']['a']
    
    哪个将输出
    4

    所以在你的例子中,你可以有一个名为“学生”的字典,每个学生都有一个科目字典,每个科目都有一个分数列表。差不多

    students = {
        'Alice': {
            'Maths': [1, 56, 23, 56],
            'Science': [23, 53, 43],
            ...
        },
        'Bob': {
            'Maths': [1, 56, 23, 56],
            'Science': [23, 53, 43],
            ...
        },
        ...
    }
    

    要获得鲍勃的第二个数学成绩,你可以使用
    学生['Bob']['math'][1]
    (不要忘记列表项从0开始索引)。

    你可以使用一个
    dict
    ,它以字符串作为键,以
    dict
    作为值:

    students = {"Alice": {"Math": [100, 98, 70], "English": [100, 97, 98]}}
    

    然后你可以像这样使用:
    学生[“Alice”][“English”][2]
    -Alice在第三次英语测试中的成绩。

    你可以使用一个
    dict
    ,它以字符串作为键,以
    dict
    s作为值:

    students = {"Alice": {"Math": [100, 98, 70], "English": [100, 97, 98]}}
    

    然后你可以这样使用:
    学生[“Alice”][“English”][2]
    -Alice在第三次英语考试中的成绩。

    类似的东西,也许:

    students = {"Alice": {"Math":    [100, 98, 89, 91, 77, 90, 82, 100, 79, 100],
                          "English": [100, 97, 98, 88, 94, 95, 98, 82, 84, 99],
                          "Science": [78, 89, 88, 92, 92, 91, 93, 88, 99, 87],
                          "French":  [100, 100, 99, 104, 103, 97, 94, 93, 75, 93]
                          }
               }
    
    这里,
    students
    是一本字典,字典的键是学生的名字(这里只显示Alice,但您可以添加更多)。每个学生都是一本字典,其关键是他或她注册的科目。这些科目是分数表。也就是说,我们不使用带有键的字典,如
    french3
    ,这些键是多余的,因为我们已经知道它们是法语的分数,我们已经知道它是第3项,因为它是第三项,我们只是将它们按顺序放在一个列表中,它们的顺序决定了访问它们的索引。(Python列表项编号以0开头,这与原始编号略有不同,但如果要显示从1开始编号的编号,则必须调整它们。)

    现在要计算Alice的数学总分,你可以写:

    sum(students["Alice"]["Math"])
    
    或者查看Alice的所有分数:

    print students["Alice"]
    
    或者看看爱丽丝的第三个法语分数:

    print students["Alice"]["French"][2]
    

    请注意:对于外部词典,最好使用
    学生
    以外的名称,因为这实际上是分数的集合。学生和科目级别就在那里,因此您可以指定您想要的分数!因此,仅仅是
    分数
    或者
    学生分数
    会是一个更好的名字。

    类似这样的名字,也许:

    students = {"Alice": {"Math":    [100, 98, 89, 91, 77, 90, 82, 100, 79, 100],
                          "English": [100, 97, 98, 88, 94, 95, 98, 82, 84, 99],
                          "Science": [78, 89, 88, 92, 92, 91, 93, 88, 99, 87],
                          "French":  [100, 100, 99, 104, 103, 97, 94, 93, 75, 93]
                          }
               }
    
    这里,
    students
    是一本字典,字典的键是学生的名字(这里只显示Alice,但您可以添加更多)。每个学生都是一本字典,其关键是他或她注册的科目。这些科目是分数表。也就是说,我们不使用带有键的字典,如
    french3
    ,这些键是多余的,因为我们已经知道它们是法语的分数,我们已经知道它是第3项,因为它是第三项,我们只是将它们按顺序放在一个列表中,它们的顺序决定了访问它们的索引。(Python列表项编号以0开头,这与原始编号略有不同,但如果要显示从1开始编号的编号,则必须调整它们。)

    现在要计算Alice的数学总分,你可以写:

    sum(students["Alice"]["Math"])
    
    或者查看Alice的所有分数:

    print students["Alice"]
    
    或者看看爱丽丝的第三个法语分数:

    print students["Alice"]["French"][2]
    

    请注意:对于外部词典,最好使用
    学生
    以外的名称,因为这实际上是分数的集合。学生和科目级别就在那里,因此您可以指定您想要的分数!因此,仅仅是
    分数
    或者
    学生分数
    可能是一个更好的名字。

    嵌套字典将在未来导致痛苦的世界。我强烈建议学习面向对象编程和
    class
    es。下面是一个不起作用但方向正确的示例

    class Assignment(dict):
        '''This inherits from a dict, student will be the key, grade as the score'''
        def __init__(self, name, max_score):
            self.name = name
            self.max_score = max_score
            self.grades = {} # this will be used to store students and grades.
    
        def __getitem__(self, student):
            # we're overriding getitem, because the default is 0 if a student doesn't turn in their work
            if student in self:
                return dict.__getitem__(self, student)
            return 0.0
    
    class GradeBook(dict):
        '''A gradebook is a dict of assignments'''
        def get_max_score(self):
            return sum(assignment.max_score for assignment in self.values())
    
        def get_percentage(self, student):
            return 100.0 * sum(assignment[student] for assignment in self.values()) / self.get_max_score()
    
    class Course(object):
        def __init__(self, name, time, teacher, students):
            self.name = name
            self.time = time # can teach multiple courses with the same name, but not at the same time
            self.teacher = teacher
            self.students = students
            self.grade_book = GradeBook()
    
        def add_assignment(self, assignemnt):
            self.grade_book.append(assignment)
    
    class Person(object):
        def __init__(self, first, last):
            self.first = first
            self.last = last
        def __repr__(self):
            return '<{} {} {}>'.format(self.__class__.__name__, self.first, self.last)
    
    
    class Student(Person):
        '''A student is a person...'''
    
    
    class Teacher(Person):
        '''A teacher is a person...'''
    
    课堂作业(dict):
    “这是从口述继承而来的,学生是关键,分数就是分数”
    定义初始(自我、姓名、最大分数):
    self.name=名称
    self.max\u分数=max\u分数
    self.grades={}#这将用于存储学生和成绩。
    定义获取项目(自我,学生):
    #我们正在重写getitem,因为如果学生不交作业,默认值为0
    如果学生本人:
    返回dict.\uuu getitem\uuu(自我,学生)
    返回0.0
    班级成绩册(dict):
    “成绩册是作业的记录”
    def get_max_分数(自我):
    返回和(赋值在self.values()中的赋值的assignment.max_分数)
    def get_百分比(自我、学生):
    返回100.0*sum(self.values()中作业的作业[学生])/self.get_max_score()
    课程类别(对象):
    定义初始(自我、姓名、时间、教师、学生):
    self.name=名称
    self.time=time#可以用同一名称教授多门课程,但不能同时教授
    self.teacher=老师
    self.students=学生
    self.grade_book=成绩册()
    def添加分配(自我、分配):
    自我评分书附加(作业)
    类人(对象):
    定义初始(自我、第一、最后):
    self.first=第一
    东南方