【问题标题】:How to print specific parts of a list using user input in Python如何使用 Python 中的用户输入打印列表的特定部分
【发布时间】:2019-02-19 14:01:15
【问题描述】:

我一直在寻找,但没有什么能完全回答我正在寻找的东西。对于这个任务,我们得到了一个列表,但我需要从该列表中打印出特定元素,使其看起来像这样,但我需要使用用户输入来搜索该列表:/

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? :")

谢谢

【问题讨论】:

  • 输入的内容是什么?
  • oH 哎呀它的名字=input("你在找谁?:")
  • 将此添加到问题并显示您的代码尝试
  • 就像打印出来的第一个代码框一样
  • @helpme 看看下面发布的答案是否有帮助?

标签: python list printing user-input element


【解决方案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')]

【讨论】:

    【解决方案2】:
    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​​

    【讨论】:

      【解决方案3】:

      你有一个元组列表。您的用户输入将是一个名称,因此基本方法是检查每个元组是否有此特定名称:

      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)
      

      【讨论】:

        【解决方案4】:

        您必须检查列表中的每个人,如果用户输入的姓名与每个人的第一项(姓名)匹配,则打印出他们的所有信息。

        另外,我建议将您的列表重命名为“列表”以外的其他名称,因为以后可能会让人感到困惑!

        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")
        

        希望这会有所帮助, -内特

        【讨论】:

          【解决方案5】:
          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 
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-10-02
            • 1970-01-01
            • 2015-12-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多