2017-09-17 16 views
0

リスト内のアイテムを削除しようとしています。 学生= [[ 'ボブ'、 '95']、[ 'グレッチェン'、 '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") 

を代わりにそれを私のリストから項目を削除しますそれは単に私にprintステートメントを与えているだけです。私は間違って何をしていますか?

+0

を試してみてください。 –

答えて

1

これは間違いでしょう。キーと値のペアを保存する場合は、辞書を使うことをお勧めします。あなたが最初にこのように辞書を定義したい:

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

を今、取り外しが簡単になる:

name = input(...) 
if name in student: 
    del student[name] 
else: 
    print("Invalid name") 

辞書の利点は、キー値に一定時間アクセスです。

0

は、あなたの質問に答えた場合、あなたは(https://stackoverflow.com/help/someone-answers)[答えを受け入れる]可能性があり、この

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") 
関連する問題