简体   繁体   English

从字典里面附加一个列表?

[英]appending to a list from inside a dictionary?

Attempting to add authors and their book titles to a list inside of a dictionary so that each author can support multiple book titles. 试图将作者及其书名添加到词典内的列表中,以便每个作者可以支持多个书名。 In the code, I have 3 authors already and each has 1 book title, but they need to be able to support at least 1 more book title. 在代码中,我已经有3位作者,每位都有1本书名,但他们需要能够支持至少1本书名。

I have the values (book titles) of the keys (authors) nested inside of a list inside the dictionary already, but I don't know how to append more values to the existing keys that are inside of the existing list. 我已经将键(作者)的值(书名)嵌套在字典内的列表中,但我不知道如何将更多值附加到现有列表内的现有键。

readings = {'George Orwell': ['1984'], 'Harper Lee': ['To Kill a Mockingbird'], 'Paul Tremblay': ['The Cabin at the End of the World']}  # list inside of dict.

I need to use the following code to append the new book titles to the list 我需要使用以下代码将新书标题附加到列表中

def add(readings):  # appending to list will go here
    author = input('\nEnter an author: ')
    if author in readings:  # check if input already inside dict.
        bookTitle = readings[author]
        print(f'{bookTitle} is already added for this author.\n')
    else:
        bookTitle = input('Enter book title: ')
        bookTitle = bookTitle.title()
        readings[author] = bookTitle
        print(f'{bookTitle} was added.\n')

I expect that you are not able to add the same book title twice and not be able to add the same author twice either. 我希望你不能两次添加相同的书名,也不能两次添加同一个作者。 I am expected to be able to input book titles for an existing author (or new author not already existing) while the program is running, then be able to view all of the authors and their book title(s) via a 'command menu' (not shown). 我希望能够在程序运行时为现有作者(或者尚未存在的新作者)输入书名,然后通过“命令菜单”查看所有作者及其书名。 (未示出)。

You're workflow is a little off. 你的工作流程有点偏差。 After checking for the author, then check for the book in the list of books by that author. 检查作者后,检查该作者的书籍清单中的书籍。 You can add a title to the list of books using .append . 您可以使用.append将标题添加到书籍列表中。 Try this: 尝试这个:

def add(readings):  # appending to list will go here
    author = input('\nEnter an author: ')
    if author in readings:  # check if input already inside dict.
        books = readings[author]
        print(f'Found {len(books)} books by {author}:')
        for b in books:
            print(f' - {b}')
    else:
        readings[author] = []

    bookTitle = input('Enter book title: ')
    bookTitle = bookTitle.title()

    if bookTitle in readings[author]:
        print(f'{bookTitle} is already added for this author.')
    else:
        readings[author].append(bookTitle)
        print(f'Add "{bookTitle}"')

So you are trying to add multiple books to an author, is that correct? 所以你试图为作者添加多本书,这是正确的吗? Since the values in your dictionary are already stored as list, you can try doing - 由于字典中的值已经存储为列表,您可以尝试 -

readings[author].append(bookTitle)

instead of 代替

readings[author] = bookTitle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM