【问题标题】:How to print out all rows that have a column equal to a certain value specified by the user?如何打印出列等于用户指定的某个值的所有行?
【发布时间】:2021-11-17 14:18:37
【问题描述】:

我正在尝试创建一些代码,用户将在其中键入类别值,如果一行中的特定列 == 该类别,那么它将以下面的格式显示该行。出于某种原因,我的代码只找到数组中与该列相等的第一行,但即使该列等于用户输入,也会跳过其余行。

例如,如果有人输入 Development 作为类别.. 它应该在第 7 列中设置 Development 的所有行(即第 1 行和第 5 行) 非常感谢任何帮助找出问题的帮助

contact_list = [[1,'Athena','Brizell','Vitz','9 Northview Hill',5888510700,344-241-3276,'Development','4/27/2021','10/29/2021','Athena Brizell'],
[2,'Isadore','Ander','Wordtune','72660 Orin Road',4848283832,877-155-7495,'Office_fitting','4/25/2021','9/26/2021','Isadore Ander'],
[3,'Hannis','Matches','Realpoint','847 Schurz Park',9052187780,368-121-1215,'Office_fitting','1/8/2021','9/4/2021','Hannis Matches'],
[4,'Cindee','Breadon','Gigashots','3 Bultman Way',4543988898,155-423-5293,'Support','4/25/2021','6/29/2021','Cindee Breadon'],
[5,'Baron','Arnao','Thoughtstorm','856 Blackbird Road',9831465576,207-486-1010,'Development','4/30/2021','10/28/2021','Baron Arnao'],
[6,'Laureen','Kearney','Dablist','70525 Texas Terrace',6091413184,602-843-9500,'Office_fitting','3/3/2021','9/3/2021','Laureen Kearney']]

category = input('Please enter the category you wish to display contacts for (Development/Office_fitting/Support: ')

for row in contact_list:
    if row[7] == category:
        firstname = f'Firstname: {row[1]}\n'
        lastname = f'Lastname: {row[2]}\n'
        company = f'Company: {row[3]}\n'
        address = f'Address: {row[4]}\n'
        landline = f'Phone: {row[5]}\n'
        mobile = f'Mobile: {row[6]}\n'
        category = f'Category: {row[7]}\n'
        date_created = f'Date Created: {row[8]}\n'
        date_modified = f'Date Updated: {row[9]}\n'
        print(firstname, lastname, company, address, landline, mobile, category, date_created, date_modified)

【问题讨论】:

    标签: python python-3.x list


    【解决方案1】:

    您更改了之前从 for 循环内的用户输入定义的 category 变量值。要修复它,请将 category 变量重命名为其他名称,例如 for 循环中的 category_row

    contact_list = [[1,'Athena','Brizell','Vitz','9 Northview Hill',5888510700,344-241-3276,'Development','4/27/2021','10/29/2021','Athena Brizell'],
    [2,'Isadore','Ander','Wordtune','72660 Orin Road',4848283832,877-155-7495,'Office_fitting','4/25/2021','9/26/2021','Isadore Ander'],
    [3,'Hannis','Matches','Realpoint','847 Schurz Park',9052187780,368-121-1215,'Office_fitting','1/8/2021','9/4/2021','Hannis Matches'],
    [4,'Cindee','Breadon','Gigashots','3 Bultman Way',4543988898,155-423-5293,'Support','4/25/2021','6/29/2021','Cindee Breadon'],
    [5,'Baron','Arnao','Thoughtstorm','856 Blackbird Road',9831465576,207-486-1010,'Development','4/30/2021','10/28/2021','Baron Arnao'],
    [6,'Laureen','Kearney','Dablist','70525 Texas Terrace',6091413184,602-843-9500,'Office_fitting','3/3/2021','9/3/2021','Laureen Kearney']]
    
    category = input('Please enter the category you wish to display contacts for (Development/Office_fitting/Support: ')
    
    for row in contact_list:
        if row[7] == category:
            firstname = f'Firstname: {row[1]}\n'
            lastname = f'Lastname: {row[2]}\n'
            company = f'Company: {row[3]}\n'
            address = f'Address: {row[4]}\n'
            landline = f'Phone: {row[5]}\n'
            mobile = f'Mobile: {row[6]}\n'
            category_row = f'Category: {row[7]}\n'
            date_created = f'Date Created: {row[8]}\n'
            date_modified = f'Date Updated: {row[9]}\n'
            print(firstname, lastname, company, address, landline, mobile, category_row, date_created, date_modified)
    

    【讨论】:

      【解决方案2】:

      那是因为你在这里修改了category

      category = f'Category: {row[7]}\n'
      

      现在包括一个新行\n 和另一个string,所以'Development' != 'Category: Development\n'

      最好在print 本身而不是在它们的绑定中格式化字符串:

      for row in contact_list:
          if row[7] == category:
              firstname = row[1]
              lastname = row[2]
              company = row[3]
              address = row[4]
              landline = row[5]
              mobile = row[6]
              category = row[7]
              date_created = row[8]
              date_modified = row[9]
              print(
                  f'Firstname: {firstname}\nLastname: {lastname}\nCompany: {company}\nAddress: {address}\nPhone: /{landline}\nMobile: {mobile}\nCategory: {category}\nDate Created: {date_created}\nDate Updated: {date_modified}')
      

      【讨论】:

      • 注意到了,感谢您的帮助
      猜你喜欢
      • 2019-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-30
      • 2021-08-02
      • 2014-07-19
      • 2017-03-04
      相关资源
      最近更新 更多