![](/img/trans.png)
[英]How to get the specific multiple value out from a list in python and If the user input is equal to the mulitple values print output
[英]How to print specific parts of a list using user input in Python
我一直在寻找,但没有完全回答我正在寻找的内容。 对于此作业,我们获得了一个列表,但我需要从该列表中打印出特定元素,使其看起来像这样,但我需要使用用户输入来搜索该列表:/
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? :")
谢谢
使用列表理解:
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')]
不要使用关键字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
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])))
结果:
你在找谁? : 杰弗里麦地那
员工姓名:杰弗里麦地那
薪资:101442.00
年龄:23
你有一个元组列表。 您的用户输入将是一个名称,因此一种基本方法是检查每个元组是否具有此特定名称:
name = input("Who are you looking for? :")
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')]
for x in lst:
if name in x:
do_something_with_this_tuple(x)
您必须检查列表中的每个人,如果用户输入的姓名与每个人的第一项(姓名)匹配,则打印出他们的所有信息。
另外,我建议将您的列表重命名为“列表”之外的其他名称,因为它稍后会变得混乱!
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")
希望这会有所帮助,-Nate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.