首页 文章

Python中的'TypeError: list indices must be integers or slices, not dict' 3.不确定为什么会发生这种情况,因为列表值是整数

提问于
浏览
-3

我正在处理一个项目,并得到一个我不明白如何纠正的类型错误 . 我正在寻找有关我所忽视的内容以及如何纠正此错误的帮助 .

下面是创建错误的函数 . 我已经打印了字典和我创建的列表,这样你们都可以看到数据是什么 . 在我看来,列表中的所有索引都是整数,这让我很困惑 .

def summarize_points(submissions):
    print(submissions[0])
    pointsPossible = []
    groupWeight = []
    userScore = []   
    for assignment in submissions:
        if assignment['workflow_state'] == 'graded':
            pointsPossible.append(int(assignment['assignment'] 
            ['points_possible']))
            groupWeight.append(int(assignment['assignment']['group'] 
            ['group_weight']))
            userScore.append(int(assignment['score']))
        pass
    pass
    print('\n\n\n\nTest Data \n\n\n')
    print('pointsPossible')
    print(pointsPossible)
    print('\ngroupWeight')
    print(groupWeight)
    print('\nuserScore')
    print(userScore)
    weightedTotalPoints = []
    weightedUserScore = []
    for assignment in submissions:
        weightedtotalPointsAddition = int(pointsPossible[assignment]) * int(groupWeight[assignment])
    weightedTotalPoints.append(weightedtotalPointsAddition)
    weightedUserScoreAddition = int(userScore[assignment]) * int(groupWeight[assignment])
    weightedUserScore.append(weightedUserScoreAddition)
currentGrade = sum(weightedUserScore) / sum(weightedTotalPoints)
currentGrade = round(currentGrade)
print(weightedtotalPoints)
print(weightedUserScore)
print(currentGrade)

下面是输出到控制台的内容

{'missing': False, 'submitted_at': '2017-08-28T23:51:13Z', 'assignment': {'due_at': '2017-08-30T16:20:00Z', 'lock_at': '2017-10-01T00:00:00Z', 'name': '#1.2) Quiz: Introduction', 'id': 270567, 'unlock_at': '2017-08-27T16:40:00Z', 'points_possible': 10.0, 'assignment_group_id': 82390, 'group': {'rules': {}, 'name': 'Learning Quizzes', 'id': 82390, 'group_weight': 25}}, 'assignment_id': 270567, 'late': False, 'attempt': 3, 'grader_id': 10926, 'workflow_state': 'graded', 'score': 9, 'graded_at': '2017-09-12T13:04:04Z', 'seconds_late': -145726, 'excused': False, 'user_id': 42}




Test Data 



pointsPossible
[10, 10, 1, 6, 10, 1, 7, 10, 4, 6, 5, 4, 7, 4, 5, 7, 5, 4, 3, 10, 2, 7, 6, 10, 7, 5, 10, 4, 6, 5, 0, 5, 16, 7, 3, 6, 5, 10, 10, 10, 5, 7, 5, 9, 4, 5, 6, 2, 21, 6, 5, 10, 3, 4, 9, 4, 6, 2, 5, 6, 17, 8, 5, 11, 4, 14, 3, 5, 7, 3, 4, 9, 4, 8, 3, 5, 3, 7, 10, 5, 10, 7, 7, 7, 10, 9, 9, 5, 5, 5]

groupWeight
[25, 25, 10, 25, 25, 25, 25, 10, 25, 10, 25, 10, 25, 25, 10, 10, 10, 25, 25, 25, 10, 25, 25, 25, 25, 10, 25, 25, 25, 25, 25, 10, 25, 25, 25, 25, 10, 25, 25, 25, 25, 25, 10, 25, 25, 25, 25, 10, 25, 25, 10, 25, 25, 25, 25, 25, 10, 10, 25, 10, 25, 25, 10, 25, 25, 25, 25, 25, 25, 10, 25, 25, 25, 25, 25, 10, 10, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 10]

userScore
[9, 9, 0, 5, 9, 0, 6, 9, 3, 5, 4, 3, 6, 3, 4, 6, 4, 3, 2, 9, 1, 7, 5, 9, 6, 4, 9, 3, 5, 4, 0, 4, 14, 6, 2, 5, 4, 9, 9, 9, 4, 6, 4, 8, 3, 4, 5, 1, 19, 5, 4, 9, 2, 3, 8, 3, 5, 1, 4, 5, 15, 7, 4, 10, 3, 13, 2, 4, 6, 2, 3, 8, 3, 7, 2, 4, 2, 6, 9, 4, 9, 6, 6, 6, 9, 8, 8, 4, 4, 4]

以下是错误

File "path", line 97, in summarize_points
weightedtotalPointsAddition = int(pointsPossible[assignment]) * 
int(groupWeight[assignment])

TypeError: list indices must be integers or slices, not dict

1 回答

  • 0

    问题很明显:你的 assignment 变量是 dict ,正如 print(submissions[0]) 所示,并且由于 pointsPossible 是一个列表,你应该输入 integerslice 来调用它的元素 . 因此,即使 listintegers 组成,当您调用 list[index] 时, index 应该包含 integerslice .

    检查你的代码,我'd guess that you'试图使用你的dict assignmentpoints_possible 的值 . 一个令人困惑的事情是,你将 submission 中的元素称为 assignment ,它与你的一个JSON键名称相同,所以这就是混乱 .

    为了从你的json中调用正确的元素,你应该调用 dictName["assignment"]["points_possible"] .

    当你调用 for 循环时,你也会将 submissions 中的每个元素调用为 assignment .

    所以正确的电话会是: assignment["assignment"]["points_possible"]

    为了达到你想要的效果,只需改变 int(pointsPossible[assignment])pointPossible[int(assignment["assignment"]["points_possible"])] 即可 .

    如果你想从 dict 中访问另一个元素,但是在你的dict中的 assignment 键内,只需将 points_possible 改为正确的 .

    PS:如果您遇到其他问题,请告诉我,我在这里更新代码 .

相关问题