关于python:从列表中删除元组

Removing tuple from list

我正在编写一个程序,允许用户输入学生记录、查看记录、删除记录并显示平均分数。我很难从列表中删除一个用户输入的名字和学生的分数。这是我迄今为止的密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
studentlist=[]
a=1
while a!=0:
    print ("""
1. Add new student records
2. Show all student records
3. Delete a student record
4. Dislay overall average coursework mark
5. Display overall average exam mark
6. Calculate average marks
0. Exit
Plese select an option
      """
)
    a=int(input(""))
    if a==1:
        name=input("Enter a students name:")
        cmark=int(input("Enter the students coursework mark:"))
        emark=int(input("Enter the students exam mark:"))
        student=(name,cmark,emark)
        print (student)
        studentlist.append(student)
        student=()
    if a==2:
        for n in studentlist:
            print ("Name:", n[0])
            print ("Courswork mark:",n[1])
            print ("Exam mark:", n[2])
            print ("")
    if a==3:
        name=input("Enter a students name:")
        for n in studentlist:
            if n[0]==name:
                studentlist.remove(n[0])
                studentlist.remove(n[1])
                studentlist.remove(n[2])


不能删除tuple的成员-必须删除整个tuple。例如:

1
2
3
x = (1,2,3)
assert x[0] == 1 #yup, works.
x.remove(0) #AttributeError: 'tuple' object has no attribute 'remove'

tuples是不变的,这意味着它们不能被改变。正如上面的错误所解释的,tuple没有remove属性/方法(它们怎么可能?它们是不变的)。

相反,尝试从上面的代码示例中删除最后三行,并用下面的行替换它们,这只会删除整个tuple

1
studentlist.remove(n)

如果您想更改或删除个别成绩(或更正学生的姓名),我建议将学生信息存储在listdict中(使用dict的示例如下)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
studentlist=[]
a=1
while a!=0:
    print ("""
1. Add new student records
2. Show all student records
3. Delete a student record
4. Dislay overall average coursework mark
5. Display overall average exam mark
6. Calculate average marks
0. Exit
Plese select an option
      """
)
    a=int(input(""))
    if a==1:
        promptdict1 = {'name': 'Enter a students name: ', \
                       'cmark': 'Enter the students coursework mark: ', \
                       'emark': 'Enter the students exam mark: '}
        studentlist.append({'name': input(promptdict1['name']), \
                            'cmark': int(input(promptdict1['cmark'])), \
                            'emark': int(input(promptdict1['emark']))})
        print(studentlist[-1])
    if a==2:
        promptdict2 = {'name': 'Name:', \
                       'cmark': 'Courswork mark:', \
                       'emark': 'Exam mark:'}
        for student in studentlist:
            print(promptdict2['name'], student['name'])
            print(promptdict2['cmark'], student['cmark'])
            print(promptdict2['emark'], student['emark'], '
'
)
    if a==3:
        name=input("Enter a students name:")
        for n in studentlist:
            if n['name']==name:
                studentlist.remove(n)

用新列表覆盖旧列表可能更有意义

1
studentList = [item for item in studentList if item[0] != name]

如果您真的想删除它,那么在迭代列表时不应该修改它…

1
2
3
4
for i,student in enumerate(studentList):
    if student[0] == name:
       studentList.pop(i)
       break #STOP ITERATING NOW THAT WE CHANGED THE LIST