Python 有没有办法在列表中找到一个字符串并只打印与该字符串关联的列表元素?
我正在使用员工管理系统,希望我的用户通过SSN搜索员工。找到员工后,我想打印与该SSN关联的列表。最后,我希望通过SSN搜索员工,并允许用户编辑字段中的数据。现在,我不知道如何将SSN搜索与列表相关联。我可以找到SSN,但我不知道如何显示它。我该怎么做Python 有没有办法在列表中找到一个字符串并只打印与该字符串关联的列表元素?,python,list,find,edit,display,Python,List,Find,Edit,Display,我正在使用员工管理系统,希望我的用户通过SSN搜索员工。找到员工后,我想打印与该SSN关联的列表。最后,我希望通过SSN搜索员工,并允许用户编辑字段中的数据。现在,我不知道如何将SSN搜索与列表相关联。我可以找到SSN,但我不知道如何显示它。我该怎么做 employee_info = [ ] while True: # While loop created to make the script constantly run counter = len(employee_info) #
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:
尝试使用这样的密钥对值,而不是dict
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
}
}
这可以让您更好地存储信息,然后更快地检索信息,而不是在数组上循环您可以解析列表中的每个员工,如果
员工[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
沿着这条线。。。。浏览列表,查看每个子列表。对于每个子列表,检查子列表[1]是否为所需的ssn。如果是这样,您就有了子列表来处理您想要的内容。这是否回答了您的问题?