如何使用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=[('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

员工名称:杰弗里·麦地那

薪水: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

  1. 请勿将关键字list用作变量名称。

  2. 您可以在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 

相关内容

  • 没有找到相关文章

最新更新