搜索嵌套列表
Searching through a nested list
我无法让我的程序在我的列表中搜索重复的 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 中第一列的 link ('01', '02',...).
您的代码不起作用,因为您正在将 id 与学生的完整记录进行比较。
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)
这样的代码可以工作,但它是递归的 - 您从内部调用 add_stu() 本身。很可能这不是您想要的。通常这样的任务是通过循环执行的,在循环中你要求用户输入并在用户输入正确时中断循环:
def add_stu(s_list):
print('Adding a student.')
while(true):
n_ID = input('ID: ')
n_NAME = input('Name: ')
n_MAJOR = input('Major: ')
n_SCORE = int(input('Score: '))
student_exists = false
for i in s_list:
if n_ID == i[0]:
student_exists = true
break
if not student_exists:
new_stu = [n_ID, n_NAME, n_MAJOR, n_SCORE]
s_list.append(new_stu)
print('Student added')
return s_list
print(f'{n_ID} already exists, unable to add student')
您正在对完整的学生条目进行比较,而不仅仅是 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。我将更改为这个(不是最有效的代码,只是尝试对现有代码进行最少的更改):
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
为了查找重复ID并进一步处理。
您可以创建一个学生 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)
我无法让我的程序在我的列表中搜索重复的 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 中第一列的 link ('01', '02',...).
您的代码不起作用,因为您正在将 id 与学生的完整记录进行比较。
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)
这样的代码可以工作,但它是递归的 - 您从内部调用 add_stu() 本身。很可能这不是您想要的。通常这样的任务是通过循环执行的,在循环中你要求用户输入并在用户输入正确时中断循环:
def add_stu(s_list):
print('Adding a student.')
while(true):
n_ID = input('ID: ')
n_NAME = input('Name: ')
n_MAJOR = input('Major: ')
n_SCORE = int(input('Score: '))
student_exists = false
for i in s_list:
if n_ID == i[0]:
student_exists = true
break
if not student_exists:
new_stu = [n_ID, n_NAME, n_MAJOR, n_SCORE]
s_list.append(new_stu)
print('Student added')
return s_list
print(f'{n_ID} already exists, unable to add student')
您正在对完整的学生条目进行比较,而不仅仅是 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。我将更改为这个(不是最有效的代码,只是尝试对现有代码进行最少的更改):
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
为了查找重复ID并进一步处理。
您可以创建一个学生 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)