As it happens, I'm assigned with a programming task in which im supposed to build a "library" of sorts. The program is supposed to load what books are available, who has written them, and if they're already loaned or not. The main menu is a menu where you can choose between several options (Search for author, search for title, loan book, return book, add book, remove book and list all books). So far I've done this using a dictionary; for the search for author/book I've scanned a text file with readlines (two different text files, one that lists the author followed by the title and one that lists the title followed by the author) and then added (e.g. if im searching for author) the author as key and the book title as value in a dictionary.
碰巧,我被分配了一个编程任务,我应该建立一个各种各样的“库”。该程序应该加载可用的书籍,谁写过它们,以及它们是否已经借出。主菜单是一个菜单,您可以在其中选择几个选项(搜索作者,搜索标题,贷款簿,还书,添加书籍,删除书籍和列出所有书籍)。到目前为止,我已经使用字典完成了这项工作;为了搜索作者/书我扫描了一个带有readlines的文本文件(两个不同的文本文件,一个列出了作者后跟标题,一个列出了标题后面跟着作者)然后添加(例如,如果我正在搜索作者)作为关键的作者和书名作为字典中的值。
However, I've now come upon a huge problem, since im using dictionaries: If an author has written several books, I can't list all books that he/she has written, only one, specifically the last one (because my loop that iterates over all the scanned lines from readlines() adds the author as key, and you can only have one identical key..). Now I wonder, is there any way that you can manipulate a dictionary so you can have several identical keys that each point to different values? Or is there a better, simpler method that can achieve what I'm trying to do?
但是,我现在遇到了一个很大的问题,因为我正在使用词典:如果作者写了几本书,我就不能列出他/她写的所有书,只列出一本,特别是最后一本(因为我的循环)迭代读取线的所有扫描线()将作者添加为键,并且您只能有一个相同的键...)。现在我想知道,有没有办法可以操作字典,这样你就可以有几个相同的键,每个键指向不同的值?或者是否有更好,更简单的方法可以实现我想要做的事情?
Extremely helpful for any help, and sorry if this seems a bit unclear. If you have any questions, please ask.
对任何帮助都非常有帮助,如果这看起来有点不清楚,那就很抱歉。如果您有任何问题,请询问。
2 个解决方案
#1
You might try using a list inside the dictionary to hold all the titles for a given author.
您可以尝试使用字典中的列表来保存给定作者的所有标题。
authors={}
authors['Brandon Sanderson'] = ['The Way of Kings', 'The Final Empire','Elantris']
That would allow you to have multiple books per author. Although given your problem description you might be better to do an dictionary of titles where each title is also a dictionary holding author/checkout info- since your searches/lookups seem to be book based not author based.
这将允许您为每位作者拥有多本书。虽然给出了你的问题描述,你可能最好做一个标题词典,其中每个标题也是一个包含作者/结账信息的字典 - 因为你的搜索/查找似乎是基于书本而不是基于作者。
titles = {}
titles['The Way of Kings'] = {'author':'Brandon Sanderson', 'checkout':False}
Update: To hold checkout info in a list just use a dictionary inside of the list. Python lists are not limited to primitives they hold all kinds of objects just fine.
更新:要在列表中保存结帐信息,只需使用列表中的字典即可。 Python列表不仅限于基元,它们可以很好地保存各种对象。
authors={}
authors['Brandon Sanderson'] = [{'title':'The Way of Kings','checkout':False},{'title':'The Final Empire','checkout':False}]
authors['Brandon Sanderson'].append({'title':'Elantris','checkout':False})
#2
Assuming you keep to your dictionary approach, you can use the author's name as the key, but insert the books as a list. Let's assume you have an author named "Smith":
假设您遵循字典方法,可以使用作者姓名作为键,但将书籍作为列表插入。假设您有一位名为“Smith”的作者:
my_dict = {}
my_dict['Smith'] = ['Book Name']
my_dict['Smith'].append('Another Book Name')
Going further, you can create a function that appends the book name to the list if the author already exists in the dictionary, and adds a new item to your dictionary if the author doesn't already exist:
更进一步,如果作者已存在于词典中,您可以创建一个将书名附加到列表中的函数,如果作者尚不存在,则将新项添加到词典中:
def addBook(Name, Title):
try:
my_dict[Name].append(Title)
except KeyError:
my_dict[Name] = [Title]
#1
You might try using a list inside the dictionary to hold all the titles for a given author.
您可以尝试使用字典中的列表来保存给定作者的所有标题。
authors={}
authors['Brandon Sanderson'] = ['The Way of Kings', 'The Final Empire','Elantris']
That would allow you to have multiple books per author. Although given your problem description you might be better to do an dictionary of titles where each title is also a dictionary holding author/checkout info- since your searches/lookups seem to be book based not author based.
这将允许您为每位作者拥有多本书。虽然给出了你的问题描述,你可能最好做一个标题词典,其中每个标题也是一个包含作者/结账信息的字典 - 因为你的搜索/查找似乎是基于书本而不是基于作者。
titles = {}
titles['The Way of Kings'] = {'author':'Brandon Sanderson', 'checkout':False}
Update: To hold checkout info in a list just use a dictionary inside of the list. Python lists are not limited to primitives they hold all kinds of objects just fine.
更新:要在列表中保存结帐信息,只需使用列表中的字典即可。 Python列表不仅限于基元,它们可以很好地保存各种对象。
authors={}
authors['Brandon Sanderson'] = [{'title':'The Way of Kings','checkout':False},{'title':'The Final Empire','checkout':False}]
authors['Brandon Sanderson'].append({'title':'Elantris','checkout':False})
#2
Assuming you keep to your dictionary approach, you can use the author's name as the key, but insert the books as a list. Let's assume you have an author named "Smith":
假设您遵循字典方法,可以使用作者姓名作为键,但将书籍作为列表插入。假设您有一位名为“Smith”的作者:
my_dict = {}
my_dict['Smith'] = ['Book Name']
my_dict['Smith'].append('Another Book Name')
Going further, you can create a function that appends the book name to the list if the author already exists in the dictionary, and adds a new item to your dictionary if the author doesn't already exist:
更进一步,如果作者已存在于词典中,您可以创建一个将书名附加到列表中的函数,如果作者尚不存在,则将新项添加到词典中:
def addBook(Name, Title):
try:
my_dict[Name].append(Title)
except KeyError:
my_dict[Name] = [Title]