有没有办法在列表中找到一个字符串并只打印与该字符串关联的列表元素?
Is there a way to find a string in a list and print just the list element associated with that string?
我正在开发员工管理系统,希望我的用户通过 SSN 搜索员工。找到员工后,我想打印与该 SSN 关联的列表。最后,我想通过 SSN 搜索员工并允许用户编辑字段中的数据。现在,我不知道如何将 SSN 搜索与列表相关联。我可以找到 SSN,但我不知道如何显示它。我该怎么做?
employee_info = [ ]
while True: # While loop created to make the script constantly run
counter = len(employee_info) # Counter created so that the user will know how many entries there are
print('There are', '(', (int(counter)), ')', 'employees in the system.\n')
add_new = input('Would you like to add a new employee to the system, find an employee\'s record by SSN, change an employee\'s information or view all employee entries in the system?\
To add an employee, type: "ADD". To view all employees currently in the system, type: "VIEW ALL." To find an employee, type: "FIND." To change employee info, type: "CHANGE."\n'\
) #Added option to add an employee, find an employee by SSN, change information or to view all employees currently the system
if add_new == 'ADD':
while True: # Loop created to input employees with option to quit
employee_index = [input('Employee Name\n'), input('Employee SSN\n'), \
input('Employee Telephone Number ***Entered as (555)555-5555*** \n'), input('Employee Email\n'), input('Employee Salary ***Entered as $xxxx***\n')]
employee_info.append(employee_index)
more_employees = input('Would you like to add another employee? Y or N.\n')
if more_employees == 'Y':
continue
elif more_employees == 'N':
break
elif add_new == 'VIEW ALL':
for employee_index in employee_info:
print(' -----------------', employee_index[0], '-----------------\n')
print('SSN:', employee_index[1], '\n')
print('Phone:', '(' + employee_index[2][0] + employee_index[2][1] + employee_index[2][2] + ')' + employee_index[2][3] + employee_index[2][4] + employee_index[2][
5] + '-' + employee_index[2][6] + employee_index[2][7] + employee_index[2][8] + employee_index[2][9], '\n')
print('Email:', employee_index[3], '\n')
print('Salary:', '$' + str(employee_index[4]), '\n')
print(' ----------------------------------------------------')
elif add_new == "FIND":
find_emp = input('Please enter the employee SSN in the following format: 333221111.\n')
if find_emp in employee_index:
尝试使用像这样的密钥对值而不是字典
mydict = {"SSN": 465736283, "Name": 'Some Dude'}
但要这样
{
"employee": {
"SSN": "1574083",
"username": "gustog",
"full_name": "Don Won",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1574083_75sq_1295469061.jpg",
"bio": "This is my bio",
"website": "http://donwon.gov",
"counts": {
"media": 1320,
"follows": 420,
"followed_by": 3410
}
}
这可能会让您更好地存储信息,然后更快地检索它,而不是循环遍历数组
您可以解析列表中的每个员工,如果employee[1] == find_emp
即如果序列号匹配,您可以打印员工的详细信息。
elif add_new == "FIND":
find_emp = input('Please enter the employee SSN in the following format: 333221111.\n')
found = False
for employee in employee_info:
if employee[1] == find_emp:
print(' -----------------', employee_index[0], '-----------------\n')
print('SSN:', employee_index[1], '\n')
print('Phone:', '(' + employee_index[2][0] + employee_index[2][1] + employee_index[2][2] + ')' + employee_index[2][3] + employee_index[2][4] + employee_index[2][
5] + '-' + employee_index[2][6] + employee_index[2][7] + employee_index[2][8] + employee_index[2][9], '\n')
print('Email:', employee_index[3], '\n')
print('Salary:', '$' + str(employee_index[4]), '\n')
print(' ----------------------------------------------------')
found = True
if found == False:
print("Employee not found!")
如果未找到该员工,您将打印相应的错误消息。
看起来您正在阅读员工索引列表,每个员工索引都是一个包含五个元素的列表,五个元素中的第二个元素是 SSN。因此,您可以 运行 遍历 5 项列表,检查 5 项列表中的第 2 项是否与 SSN 匹配。所以...
def match_the_ssn(list_of_lists, ssn):
for item in list_of_lists:
try:
if item[1] == ssn:
print(item)
return item
except:
pass
return
类似的东西.... 运行 遍历列表,查看每个子列表。对于每个子列表,检查 sublist[1] 是否是您想要的 ssn。如果是这样,您的子列表就在那里,可以满足您的需求。
我正在开发员工管理系统,希望我的用户通过 SSN 搜索员工。找到员工后,我想打印与该 SSN 关联的列表。最后,我想通过 SSN 搜索员工并允许用户编辑字段中的数据。现在,我不知道如何将 SSN 搜索与列表相关联。我可以找到 SSN,但我不知道如何显示它。我该怎么做?
employee_info = [ ]
while True: # While loop created to make the script constantly run
counter = len(employee_info) # Counter created so that the user will know how many entries there are
print('There are', '(', (int(counter)), ')', 'employees in the system.\n')
add_new = input('Would you like to add a new employee to the system, find an employee\'s record by SSN, change an employee\'s information or view all employee entries in the system?\
To add an employee, type: "ADD". To view all employees currently in the system, type: "VIEW ALL." To find an employee, type: "FIND." To change employee info, type: "CHANGE."\n'\
) #Added option to add an employee, find an employee by SSN, change information or to view all employees currently the system
if add_new == 'ADD':
while True: # Loop created to input employees with option to quit
employee_index = [input('Employee Name\n'), input('Employee SSN\n'), \
input('Employee Telephone Number ***Entered as (555)555-5555*** \n'), input('Employee Email\n'), input('Employee Salary ***Entered as $xxxx***\n')]
employee_info.append(employee_index)
more_employees = input('Would you like to add another employee? Y or N.\n')
if more_employees == 'Y':
continue
elif more_employees == 'N':
break
elif add_new == 'VIEW ALL':
for employee_index in employee_info:
print(' -----------------', employee_index[0], '-----------------\n')
print('SSN:', employee_index[1], '\n')
print('Phone:', '(' + employee_index[2][0] + employee_index[2][1] + employee_index[2][2] + ')' + employee_index[2][3] + employee_index[2][4] + employee_index[2][
5] + '-' + employee_index[2][6] + employee_index[2][7] + employee_index[2][8] + employee_index[2][9], '\n')
print('Email:', employee_index[3], '\n')
print('Salary:', '$' + str(employee_index[4]), '\n')
print(' ----------------------------------------------------')
elif add_new == "FIND":
find_emp = input('Please enter the employee SSN in the following format: 333221111.\n')
if find_emp in employee_index:
尝试使用像这样的密钥对值而不是字典
mydict = {"SSN": 465736283, "Name": 'Some Dude'}
但要这样
{
"employee": {
"SSN": "1574083",
"username": "gustog",
"full_name": "Don Won",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1574083_75sq_1295469061.jpg",
"bio": "This is my bio",
"website": "http://donwon.gov",
"counts": {
"media": 1320,
"follows": 420,
"followed_by": 3410
}
}
这可能会让您更好地存储信息,然后更快地检索它,而不是循环遍历数组
您可以解析列表中的每个员工,如果employee[1] == find_emp
即如果序列号匹配,您可以打印员工的详细信息。
elif add_new == "FIND":
find_emp = input('Please enter the employee SSN in the following format: 333221111.\n')
found = False
for employee in employee_info:
if employee[1] == find_emp:
print(' -----------------', employee_index[0], '-----------------\n')
print('SSN:', employee_index[1], '\n')
print('Phone:', '(' + employee_index[2][0] + employee_index[2][1] + employee_index[2][2] + ')' + employee_index[2][3] + employee_index[2][4] + employee_index[2][
5] + '-' + employee_index[2][6] + employee_index[2][7] + employee_index[2][8] + employee_index[2][9], '\n')
print('Email:', employee_index[3], '\n')
print('Salary:', '$' + str(employee_index[4]), '\n')
print(' ----------------------------------------------------')
found = True
if found == False:
print("Employee not found!")
如果未找到该员工,您将打印相应的错误消息。
看起来您正在阅读员工索引列表,每个员工索引都是一个包含五个元素的列表,五个元素中的第二个元素是 SSN。因此,您可以 运行 遍历 5 项列表,检查 5 项列表中的第 2 项是否与 SSN 匹配。所以...
def match_the_ssn(list_of_lists, ssn):
for item in list_of_lists:
try:
if item[1] == ssn:
print(item)
return item
except:
pass
return
类似的东西.... 运行 遍历列表,查看每个子列表。对于每个子列表,检查 sublist[1] 是否是您想要的 ssn。如果是这样,您的子列表就在那里,可以满足您的需求。