我一直在搜索,但没有任何答案能满足我的需求。对于此分配,我们得到了一个列表,但是我需要从该列表中打印出特定元素,以便看起来像这样,但是我需要使用用户输入来搜索该列表:/
Employee Name: Jeffery Medina
Salary: 101442.00
Age: 23
这是我的清单
lst=[('Jeffery Medina','Officer','1254','101442.00','23'),('Katrina Peters','Officer','3423','94122.00','45'),('Kim Alan','Captain','6434','101592.00','29'),('Vincente Mendez','Officer','3235','110064.00','32'),('Chris Boalen','Captain','8769','50436.00','56'),('James Vito','Clerk','4451','23500.00','61'),('Terry George','Fireman','3342','93354.00','32'),('Zaid Dane','Officer','2345','84054.00','19'),('Ernesto Rodriguez','Officer','9091','87006.00','35'),('Josefine White','Fireman','3401','102228.00','26'),('Mario Infante','Officer','3234','84054.00','22'),('Juan Almonte','Fireman','4103','91272.00','50'),('Kevin Smith','Fireman','3450','111492.00','62'),('Abdum Smith','Captain','2234','95484.00','20'),('Juan Gomez','Clerk','9023','23890.00','49')]
对于用户输入,我只是使用了这个:
name=input("Who are you looking for? :")
谢谢
答案 0 :(得分:1)
具有列表理解功能:
data = [('Jeffery Medina','Officer','1254','101442.00','23'),('Katrina Peters','Officer','3423','94122.00','45'),('Kim Alan','Captain','6434','101592.00','29'),('Vincente Mendez','Officer','3235','110064.00','32'),('Chris Boalen','Captain','8769','50436.00','56'),('James Vito','Clerk','4451','23500.00','61'),('Terry George','Fireman','3342','93354.00','32'),('Zaid Dane','Officer','2345','84054.00','19'),('Ernesto Rodriguez','Officer','9091','87006.00','35'),('Josefine White','Fireman','3401','102228.00','26'),('Mario Infante','Officer','3234','84054.00','22'),('Juan Almonte','Fireman','4103','91272.00','50'),('Kevin Smith','Fireman','3450','111492.00','62'),('Abdum Smith','Captain','2234','95484.00','20'),('Juan Gomez','Clerk','9023','23890.00','49')]
name = input('Who are you looking for: ')
print([x for x in data if name in x[0]])
输出:
Who are you looking for: Jeffery
[('Jeffery Medina', 'Officer', '1254', '101442.00', '23')]
答案 1 :(得分:0)
请勿将关键字list
用作变量名。
您可以遍历list
的长度,并检查name
是否等于其第一个索引的第一个元素。
因此:
lst=[('Jeffery Medina','Officer','1254','101442.00','23'),('Katrina Peters','Officer','3423','94122.00','45'),('Kim Alan','Captain','6434','101592.00','29'),('Vincente Mendez','Officer','3235','110064.00','32'),('Chris Boalen','Captain','8769','50436.00','56'),('James Vito','Clerk','4451','23500.00','61'),('Terry George','Fireman','3342','93354.00','32'),('Zaid Dane','Officer','2345','84054.00','19'),('Ernesto Rodriguez','Officer','9091','87006.00','35'),('Josefine White','Fireman','3401','102228.00','26'),('Mario Infante','Officer','3234','84054.00','22'),('Juan Almonte','Fireman','4103','91272.00','50'),('Kevin Smith','Fireman','3450','111492.00','62'),('Abdum Smith','Captain','2234','95484.00','20'),('Juan Gomez','Clerk','9023','23890.00','49')]
name = input("Who are you looking for? :")
for i in range(len(lst)):
try:
if name == lst[i][i]:
print("Employe Name: {} \nSalary: {} \nAge: {} ".format(lst[i][i], lst[i][i+3], lst[i][i+4]))
except IndexError:
pass
输出:
Who are you looking for? :Jeffery Medina
Employe Name: Jeffery Medina
Salary: 101442.00
Age: 23
编辑:
使用正则表达式的另一种解决方案将消除区分大小写的需求。
for i in range(len(lst)):
try:
if re.search(name, lst[i][i], re.IGNORECASE):
print("Employe Name: {} \nSalary: {} \nAge: {} ".format(lst[i][i], lst[i][i+3], lst[i][i+4]))
except IndexError:
pass
输出:
Who are you looking for? :jeFFerY mEdinA
Employe Name: Jeffery Medina
Salary: 101442.00
Age: 23
答案 2 :(得分:0)
list=[('Jeffery Medina','Officer','1254','101442.00','23'),('Katrina Peters','Officer','3423','94122.00','45'),('Kim Alan','Captain','6434','101592.00','29'),('Vincente Mendez','Officer','3235','110064.00','32'),('Chris Boalen','Captain','8769','50436.00','56'),('James Vito','Clerk','4451','23500.00','61'),('Terry George','Fireman','3342','93354.00','32'),('Zaid Dane','Officer','2345','84054.00','19'),('Ernesto Rodriguez','Officer','9091','87006.00','35'),('Josefine White','Fireman','3401','102228.00','26'),('Mario Infante','Officer','3234','84054.00','22'),('Juan Almonte','Fireman','4103','91272.00','50'),('Kevin Smith','Fireman','3450','111492.00','62'),('Abdum Smith','Captain','2234','95484.00','20'),('Juan Gomez','Clerk','9023','23890.00','49')]
name = input("Who are you looking for? :")
for i in range(len(list)):
if list[i][0] == name:
print("Employe Name: {} \nSalary: {} \nAge: {} ".format(name,str(list[i][3]),str(list[i][4])))
结果:
您在寻找谁? :杰弗里·麦地那(Jeffery Medina)
员工姓名:Jeffery Medina
工资:101442.00
年龄:23
答案 3 :(得分:0)
您有一个元组列表。您的用户输入将是一个名称,因此一种基本方法是检查每个元组是否有此特定名称:
CascadeType.ALL
答案 4 :(得分:0)
您必须检查列表中的每个人,如果用户输入的名称与每个人的第一项(名称)相匹配,请打印出他们的所有信息。
此外,我建议您将列表重命名为“列表”以外的名称,因为以后可能会造成混淆!
while True:
name = input("Who are you looking for?: ")
for person in people:
if person[0] == name:
print("Name: {},\nRank: {},\nNumber: {},\nSalary: {},\nAge: {}".format(person[0],person[1],person[2],person[3],person[4]))
break
else:
print("This person does not exist. Try Another")
希望这会有所帮助, -内特