【问题标题】:Returning only one value from a dictionary in Python从 Python 的字典中只返回一个值
【发布时间】:2018-08-05 01:02:00
【问题描述】:

我无法使用循环仅返回字典中的值。我不知道如何不从字典中返回不需要的值。

我的字典在下面。我希望用户输入书名,我的代码将返回相应的 ISBN。

library = {1234567891234: [4,'Salems Lot','Stephen King'],
       2345678912345: [1,'Pride and Prejudice','Jane Austen'],
       3456789123456: [6,'Moby Dick','Herman Melville']}


def book_search():
    book_title = input("What book are you searching for? ")
    for k, v in library.items():
        if v[1].lower() == book_title:
            print("The ISBN of", book_title, "is", k)
        else:
            print("This book is not in the library")

但我的程序返回字典中的所有值。我怎样才能让它只返回特定的 ISBN?以下是我目前得到的。

What book are you searching for? salems lot
The ISBN of salems lot is 1234567891234
This book is not in the library
This book is not in the library

我能得到一些帮助吗?

【问题讨论】:

  • 如果不希望在标题不匹配的情况下打印,为什么要在标题不匹配的情况下显式打印?
  • 和@jon 所说的一样 - 如果您要经常进行该查找,您应该考虑创建 title->other_info 的反向映射...
  • @jonrsharpe 这是一个很好的观点。我不需要这样做。我只需要实现一些异常处理。谢谢
  • 在开始时创建一个标志布尔值并将其设置为 False。找到所需的项目后,将其更改为 True 并使用“break”关键字中断 for 循环。然后不是每次都打印这本书不在图书馆,而是在循环之外只写 - if flag == False: print(''This book is not in the library")
  • @Crawley 这不是一种非常惯用的方式。通常你会使用for: ... else: ... 结构,或者在肯定的情况下使用早期的return,然后将“未找到”逻辑放在循环之外

标签: python list dictionary for-loop key


【解决方案1】:

当您使用循环搜索条目时,您应该在找到时退出循环。并考虑它仅在循环之后不存在(不是在每次迭代中)。

以下代码应该可以工作:

library = {1234567891234: [4, 'Salems Lot', 'Stephen King'],
           2345678912345: [1, 'Pride and Prejudice', 'Jane Austen'],
           3456789123456: [6, 'Moby Dick', 'Herman Melville']}


def book_search():
    book_title = input("What book are you searching for? ")
    found = False
    for k, v in library.items():
        if v[1].lower() == book_title.lower():
            print("The ISBN of", book_title, "is", k)
            # We have already found a match, so should exit of the loop.
            found = True
            break

    if not found:
        print("This book is not in the library")


book_search()

【讨论】:

    【解决方案2】:

    您对printreturn 感到困惑。

    print,顾名思义,将在您的提示中显示参数。

    return 将停止该函数,并将您发送到 return 的值退出该函数。

    在这里,如果您只是将print 更改为return,并且如果您只是调用您的函数,例如...

    # Instead of "book_search()", do...
    print book_search()
    

    你会有被问到的行为。

    • 您的 else 条件可能处于错误的范围内。我相信你可能想要它在其他空间。

    【讨论】:

      【解决方案3】:

      我认为这应该可以解决您的问题:

      `

        library = {1234567891234: [4,'Salems Lot','Stephen King'], 2345678912345: [1,'Pride and Prejudice','Jane Austen'], 3456789123456: [6,'Moby Dick','Herman Melville']} 
      
      
       def book_search(): 
      
          j=0
      
          book_title = input("What book are you searching for? ") 
          for k, v in library.items():
                 if v[1].lower() == book_title:
                      j=j+1
                      print("The ISBN of", book_title, "is", k)
          if(j==0)
                  print("This book is not in the library")`
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-30
        • 2017-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多