搜索嵌套列表

时间:2020-03-28 05:44:53

标签: python list nested-lists

我无法让我的程序在列表中搜索重复的ID。我需要将一个新学生添加到我的列表中,但是如果输入的ID已被使用,则程序需要打印该ID已被使用并且无法添加到列表中。我的清单:

ID, NAME, MAJOR, SCORE = 0, 1, 2, 3
s_list = [
    ['01', 'Smith', 'CS', 100],
    ['02', 'Jones', 'CS', 90],
    ['03', 'Anderson', 'Math', 80],
    ['04', 'Johnson', 'Bio', 99],
]
def stu_list(s_list):
    print('Student List:')
    print('Id'.ljust(5), 'Name'.ljust(12), 'Major'.ljust(9), 'Score')
    for ID, NAME, MAJOR, SCORE in s_list:
        print(f'{ID:6}{NAME:13}{MAJOR:8}{SCORE:5}')
    print('--End of List--\n')

已更新:

def insert_stu(s_list):
    print('Adding a student.')
    n_ID = input('ID: ')
    n_NAME = input('Name: ')
    n_MAJOR = input('Major: ')
    n_SCORE = int(input('Score: '))

    id_list = []
    for stud in s_list:
        id_list.append(stud[0])

        if n_ID in id_list:
            print(f'{n_ID} already exists, unable to add student')
        elif n_ID not in s_list:
                new_stu = [n_ID, n_NAME, n_MAJOR, n_SCORE]
                s_list.append(new_stu)
                print(f'Not Found \nAdding to the list.')
        stu_list(s_list)
        return insert_stu(s_list)

输出:

Adding a student.
ID: 02
Name: Cris
Major: math
Score: 23
Not Found 
Adding to the list.
Student List:
Id    Name         Major     Score
01    Smith        CS        100
02    Jones        CS         90
03    Anderson     Math       80
04    Johnson      Bio        99
09    new          cs         90
02    Cris         math       23   ##still adding duplicate id's
--End of List--

运行该功能时,无论输入的ID是否已在使用中,它都会添加新学生。我认为我遇到的问题是让ID变量链接到s_list的第一列(“ 01”,“ 02”,...)。

3 个答案:

答案 0 :(得分:1)

您的代码无效,因为您正在将ID与有关该学生的完整记录进行比较。

{"name":"john", "age":"19" }
{"college":"abc", "degree":"PHD"}
[{ "country" : "india", "mobile" : "1234567890"}]

这样的代码可以工作,但是它是递归的-您可以从内部调用add_stu()。很可能不是您想要的。通常,此类任务是通过循环执行的,在循环中,您要求用户输入并在用户输入正确的情况下中断循环:

for i in s_list:      # i is the full record about the student   
   if n_ID == i[0]:   # i[0] is the student's id
      print(f'{n_ID} already exists, unable to add student')
      return add_stu(s_list)

# If the for loop finished, then no match was found
s_list.append(new_stu)
stu_list(s_list)
print('Student added')
return add_stu(s_list) 

答案 1 :(得分:1)

您正在对完整的学生条目进行比较,而不仅仅是ID。 您可以通过更改以下代码来解决:

for i in s_list:            ##not searching list for duplicate
    if n_ID == i:

收件人:

for i in s_list:            ##not searching list for duplicate
    if n_ID == i[0]:

或仅使用字典而不是列表。

编辑 您的第二个版本在检查的同时构建了id列表,因此for循环添加了现有的学生,因为它尚未将其添加到id列表中。您要添加ID 02,因为循环的第一次迭代中的ID是01。我将更改为该ID(不是最有效的代码,只是尝试对现有代码进行最小的更改):

for stud in s_list:
    id_list.append(stud[0])

if n_ID in id_list:
    print(f'{n_ID} already exists, unable to add student')
elif n_ID not in s_list:
    new_stu = [n_ID, n_NAME, n_MAJOR, n_SCORE]
    s_list.append(new_stu)
    print(f'Not Found \nAdding to the list.')
stu_list(s_list)
return s_list

答案 2 :(得分:1)

为了搜索重复的ID并进行进一步处理。

您可以创建学生证的列表,然后检查该列表中的重复项。

def insert_stu(s_list):
    print('Adding a student.')
    n_ID = input('ID: ')
    n_NAME = input('Name: ')
    n_MAJOR = input('Major: ')
    n_SCORE = int(input('Score: '))

    id_list = []
    for stud in s_list:      # creating list of ID
        id_list.append(stud[0])

    if n_ID in id_list:      # searching list for duplicate
        print(f'{n_ID} already exists, unable to add student')
    elif n_ID not in s_list:
        new_stu = [n_ID, n_NAME, n_MAJOR, n_SCORE]
        s_list.append(new_stu)
        print(f'Not Found \nAdding to the list.')
    stu_list(s_list)
    return insert_stu(s_list)

相关问题