[英]How to print specific parts of a list using user input in Python
I been searching but nothing quite answers what I'm looking for.我一直在寻找,但没有完全回答我正在寻找的内容。 For this assignment we were given a list, but I need to print out specific elements from that list so it could look something like this, but I need to use user input to search that list :/
对于此作业,我们获得了一个列表,但我需要从该列表中打印出特定元素,使其看起来像这样,但我需要使用用户输入来搜索该列表:/
Employee Name: Jeffery Medina
Salary: 101442.00
Age: 23
This is my list这是我的清单
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')]
And for user input i just used this: name=input("Who are you looking for? :")
对于用户输入,我只是使用了这个:
name=input("Who are you looking for? :")
Thanks谢谢
With list comprehension:使用列表理解:
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]])
Output:输出:
Who are you looking for: Jeffery
[('Jeffery Medina', 'Officer', '1254', '101442.00', '23')]
Do not use the keyword list
as a variable name.不要使用关键字
list
作为变量名。
You could iterate over the length of the list
and check if the name
is equal to any of its first index's first element.您可以遍历
list
的长度并检查name
是否等于其第一个索引的第一个元素。
Hence:因此:
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
OUTPUT:输出:
Who are you looking for? :Jeffery Medina
Employe Name: Jeffery Medina
Salary: 101442.00
Age: 23
EDIT:编辑:
Another solution, using regex that would eliminate the need of case sensitivity.另一种解决方案,使用正则表达式可以消除区分大小写的需要。
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
OUTPUT:输出:
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])))
Result:结果:
Who are you looking for?你在找谁? : Jeffery Medina
: 杰弗里麦地那
Employee Name: Jeffery Medina员工姓名:杰弗里麦地那
Salary: 101442.00薪资:101442.00
Age: 23年龄:23
You have a list of tuples.你有一个元组列表。 Your user input would be a name, so a basic approach would be to check every tuple for this specific name:
您的用户输入将是一个名称,因此一种基本方法是检查每个元组是否具有此特定名称:
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)
You have to check each person in the list, and if the name of the user input matches the first item of each person (name), print out all their info.您必须检查列表中的每个人,如果用户输入的姓名与每个人的第一项(姓名)匹配,则打印出他们的所有信息。
Also, I would suggest renaming your list to something besides “list” since it can get confusing later on!另外,我建议将您的列表重命名为“列表”之外的其他名称,因为它稍后会变得混乱!
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")
Hope this helps, -Nate希望这会有所帮助,-Nate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.