首页 > 解决方案 > 'TypeError: list indices must be integers or slices, not dict' in Python 3. Unsure of why this is happening as list values are integers

问题描述

I am working on a project and getting a type error that I do not understand how to correct. I am looking for help on what I am overlooking and how to correct this error.

Below is the function that is creating the error. I have printed the dictionary and the lists I have created so that you all can see what the data is. It seems to me like all of the indices in the list ARE integers and that is what is confusing me.

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)

Below this is what is output to the console

{'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]

Below is the error

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

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

标签: pythonpython-3.xlistdictionarytypeerror

解决方案


问题很清楚:您的assignment变量是 a dict,如您所print(submissions[0])展示的,并且由于pointsPossible是列表,因此您应该输入 aninteger或 aslice以从中调用元素。因此,即使您list由 组成integers,当您调用时list[index]index应该包含一个integeror slice

检查您的代码,我猜您正在尝试使用points_possibledict 中的值assignment。一件令人困惑的事情是,您从submissionas中调用了元素assignment,这与您的一个 JSON 键的名称相同,所以这就是混乱。

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

当你调用循环时for,你也在调用每个元素。submissionsassignment

所以正确的调用是: assignment["assignment"]["points_possible"]

为了实现你想要的,只要改变int(pointsPossible[assignment])pointPossible[int(assignment["assignment"]["points_possible"])]可以了。

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

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


推荐阅读