我被要求解决以下问题,但无法提供正确的代码:
此练习涉及构建非平凡的字典。主题是书籍。
每本书的关键是书名 与该键关联的值是字典
在该词典中将有三个键:它们都是字符串,它们是:
"Pages", "Author", "Publisher"
"Pages"
与一个值关联-int
"Author"
与字典关联为值
该"Author"
字典有两个键:"First"
和"Last"
每个都有一个字符串值
"Publisher"
与字典关联为值
该"Publisher"
字典有一个键"Location"
,其中有一个字符串作为值。
示例可能如下:
{"Harry Potter": {"Pages":200, "Author":{"First":"J.K", "Last":"Rowling"}, "Publisher":{"Location":"NYC"}},
"Fear and Lothing in Las Vegas": { ...}}
编写一个名为"build_book_dict"
的函数
接受五个输入,所有列表均为n长度
标题,页面,第一,最后和位置的列表。
如上所述返回字典。 键的拼写必须与上方显示的一样-正确且大写。
这里是一个例子:
titles = ["Harry Potter", "Fear and Lothing in Las Vegas"]
pages = [200, 350]
firsts = ["J.K.", "Hunter"]
lasts = ["Rowling", "Thompson"]
locations = ["NYC", "Aspen"]
book_dict = build_book_dict(titles, pages, firsts, lasts, locations)
print(book_dict)
{'Fear and Lothing in Las Vegas': {'Publisher': {'Location': 'Aspen'}, 'Author': {'Last': 'Thompson', 'First': 'Hunter'}, 'Pages': 350} 'Harry Potter': {'Publisher': {'Location': 'NYC'},'Author': {'Last': 'Rowling', 'First': 'J.K.'}, 'Pages': 200}}
我的代码当前看起来如下:
def build_book_dict(titles, pages, firsts, lasts, locations):
inputs = zip(titles, pages, firsts, lasts, locations)
for titles, pages, firsts, lasts, locations in inputs:
dict = {
titles : {
"Pages" : pages,
"Author" : {
"First" : first,
"Last" : last
},
"Publisher" : {
"Location" : locations
},
},
}
return dict
但是它仅存储最后一本“书”的信息。
答案 0 :(得分:2)
使用此功能,主要更改是我添加了d.update
:
def build_book_dict(titles, pages, firsts, lasts, locations):
inputs = zip(titles, pages, firsts, lasts, locations)
d = {}
for titles, pages, firsts, lasts, locations in inputs:
d.update({
titles : {
"Pages" : pages,
"Author" : {
"First" : firsts,
"Last" : lasts
},
"Publisher" : {
"Location" : locations
},
},
})
return d
现在:
print(build_book_dict(titles, pages, firsts, lasts, locations))
成为:
{'Harry Potter': {'Pages': 200, 'Author': {'First': 'J.K.', 'Last': 'Rowling'}, 'Publisher': {'Location': 'NYC'}}, 'Fear and Lothing in Las Vegas': {'Pages': 350, 'Author': {'First': 'Hunter', 'Last': 'Thompson'}, 'Publisher': {'Location': 'Aspen'}}}
您的代码不起作用,因为您每次都在创建一个新词典,而不是将字典加在一起,但是d.update
克服了这个问题。
此外,我将变量dict
重命名为d
,因为dict
是默认关键字,而当您命名变量dict
时,您将无法来访问实际的dict
关键字。
答案 1 :(得分:0)
在for循环之前创建一个空字典。然后在循环中像这样添加它:
def build_book_dict(titles, pages, firsts, lasts, locations):
inputs = zip(titles, pages, firsts, lasts, locations)
d = dict()
for titles, pages, firsts, lasts, locations in inputs:
d[titles] = {whatever}
return d
答案 2 :(得分:0)
您可以使用字典理解:
titles = ["Harry Potter", "Fear and Lothing in Las Vegas"]
pages = [200, 350]
firsts = ["J.K.", "Hunter"]
lasts = ["Rowling", "Thompson"]
locations = ["NYC", "Aspen"]
data = {a:{'Pages':b, 'Author':{'First':c, 'Last':d}, 'Publisher': {'Location': e}} for a, b, c, d, e in zip(titles, pages, firsts, lasts, locations)}
输出:
{'Harry Potter': {'Pages': 200, 'Author': {'First': 'J.K.', 'Last': 'Rowling'}, 'Publisher': {'Location': 'NYC'}}, 'Fear and Lothing in Las Vegas': {'Pages': 350, 'Author': {'First': 'Hunter', 'Last': 'Thompson'}, 'Publisher': {'Location': 'Aspen'}}}