List 如何在python的字典中存储多值

List 如何在python的字典中存储多值,list,file-io,split,List,File Io,Split,我正试着让这本字典查一下 theInventory = {} 对以下项目进行排序,以便我可以添加价格或查看作者的书籍等 这是我的家庭作业问题。所以我需要使用字典和使用2D列表的多值 让名为database.txt的文本文件包含 last name/first name/quantity/price Shakespeare,William,Romeo And Juliet,5,5.99 Shakespeare,William,Macbeth,3,7.99 Dickens,Charles,H

我正试着让这本字典查一下

theInventory = {}
对以下项目进行排序,以便我可以添加价格或查看作者的书籍等

这是我的家庭作业问题。所以我需要使用字典和使用2D列表的多值

让名为database.txt的文本文件包含

last name/first name/quantity/price

Shakespeare,William,Romeo And Juliet,5,5.99

Shakespeare,William,Macbeth,3,7.99

Dickens,Charles,Hard Times,7,27.00

Austin,Jane,Sense And Sensibility,2,4.95
是否可以执行以下操作

inFile = (database.txt, "r")
For line in inFile:
    aLine = []
    aLine = line.split(",") # dont know how to split by ,
    theInventory[aLine[0] + ", " + aLine[1]] = list[list[aLine[3], int(aLine[4]), float(aLine[5])]]

inFile.close()
结果会是这样

>print (theInventory)
>> {"Shakespeare, William": [["Romeo And Juliet", 5, 5.99], ["Macbeth", 3, 7.99]], "Dickens, Charles": [["Hard Times", 7, 27.00]], "Austin, Jane": [["Sense And Sensibility", 2, 4.95]]}
这样您就可以修改特定书籍的数量和价格


甚至可以将书籍添加到字典。

该文件看起来像一个CSV文件。Python有一个名为csv的模块,它使用户能够读/写rcsv文件。 下面是python文档中给出的示例代码

import csv
with open('database.txt', 'rb') as f:
    reader = csv.reader(f, delimiter=',', quoting=csv.QUOTE_NONE)
    for row in reader:
        print row
输出:

['Shakespeare', 'William', 'Romeo And Juliet', '5', '5.99']
['Shakespeare', 'William', 'Macbeth', '3', '7.99']
['Dickens', 'Charles', 'Hard Times', '7', '27.00']
['Austin', 'Jane', 'Sense And Sensibility', '2', '4.95']
>>> 
{'Dickens,Charles': [['Hard Times', '7', '27.00']], 'Shakespeare,William': [['Romeo And Juliet', '5', '5.99'], ['Macbeth', '3', '7.99']], 'Austin,Jane': [['Sense And Sensibility', '2', '4.95']]}
有关csv模块的更多信息可在此处找到:

编辑:这是一个非常基本的东西,但可能会给你一个想法

导入csv

dicto = {}
name = ''
#dicto[name] = []
with open('database.txt', 'rb') as f:
    reader = csv.reader(f, delimiter=',', quoting=csv.QUOTE_NONE)
    for row in reader:
        name = row[0] + ',' + row[1] # frames the name
        if name in dicto.keys(): # checks if the name exists in the dictionary , if yes just append the book 
            books = dicto.get(name)
            books.append(row[2:])
        else: # if no entry of author is made, create new book list
            books = []
            books.append(row[2:])
        dicto[name] =books # update the list of books

    print dicto
输出:

['Shakespeare', 'William', 'Romeo And Juliet', '5', '5.99']
['Shakespeare', 'William', 'Macbeth', '3', '7.99']
['Dickens', 'Charles', 'Hard Times', '7', '27.00']
['Austin', 'Jane', 'Sense And Sensibility', '2', '4.95']
>>> 
{'Dickens,Charles': [['Hard Times', '7', '27.00']], 'Shakespeare,William': [['Romeo And Juliet', '5', '5.99'], ['Macbeth', '3', '7.99']], 'Austin,Jane': [['Sense And Sensibility', '2', '4.95']]}

谢谢你提供的信息,但我需要使用字典,所以在以后的编程中,当我给作者莎士比亚打电话时,它会显示罗密欧和朱丽叶以及麦克白的信息。比如一个键:值我想你想要一个我希望的,但是对于这个项目,我的教授希望我们有一个字典的2D列表。在未来,一定要指出你在问一个家庭作业问题。好的,谢谢你的建议