如何从元组列表中删除其中一个学生?我对这部分很困惑,不知道从哪里开始。任何人都可以帮我指出正确的方向吗?
select=True
data=[]
while select:
print ("""
student Database (student records:)
a. Delete a student from the database
""" )
select=input("What would you like to do? ")
if select == "1":
print("Add a new student")
student = input ("Enter studenbt:")
grades = input ("Enter grade:")
entermark = float (input ("enter mark:"))
fulltime = input ("Is the student full-time? Yes/No:")
data.append((student, grades, entermark, ))
print ("The student you have entered is:",data)
elif select == "a":
delete = input("student do delete?")
答案 0 :(得分:1)
从您的数据而不是list
创建dict
。然后,您只需在常量时间内使用data.pop(key)
删除所需的项目。
data = {}
# this is how you add data
data['studentName1'] = (10,20)
data['studentName2'] = (32,42)
# this is how you remove data
data.pop('studentName1')
答案 1 :(得分:0)
如果您使用pop()
方法知道其索引,则可以删除列表中的元素。
例如:
>>> data.pop(5)
将删除第5个元素。
在您的情况下,您需要找到要删除的学生的索引,然后使用pop()
将其删除。
例如,您可以通过遍历列表来找到学生:
delete = input("Which employee would you like to delete from the database?")
for index, row in enumerate(data):
if row[0] == delete:
data.pop(index)
break