在嵌套列表中访问要删除的项目?

时间:2017-09-17 23:12:29

标签: python list nested-lists

我正在尝试删除列表中的项目。 学生= [[' Bob',' 95'],[' Gretchen',' 90']

我的代码如下:

 elif menu == 3:
        # Remove students
        student_removal = input("Please enter the name of the student you would like to remove")
        for s in student:
            if isinstance(student, type(list)):
                student.remove(student_removal)
        if student != student_removal:
            print("The student name you have entered is not in the grade book. Please choose a different option")

而不是从列表中删除项目,它只是给我打印声明。我做错了什么?

2 个答案:

答案 0 :(得分:1)

你可能会犯这个错误。如果你要存储键值对,我建议使用字典。您首先要定义这样的字典:

return float(total) / float(ratings);

现在,删除变得简单了:

student = { 'Bob' : '95', 'Gretchen' : '90' } 

字典的优点是可以持续访问键值。

答案 1 :(得分:0)

试试这个

student_removal = input("Please enter the name of the student you would like to remove")
if student_removal in student:
    student.remove(student_removal)
    print("Student removed from list")
else:
    print("The student name you have entered is not in the grade b")