簡體   English   中英

如何使用 Python 中的用戶輸入打印列表的特定部分

[英]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')]
  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 
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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM