我试图通过代码中所示的函数使用jupyter笔记本返回字典。我是Python的初学者,不知道该怎么做,但我觉得答案很简单
我不确定是否需要for循环或语句
def build_book_dict(titles, pages, firsts, lasts, locations):
if True:
return dict()
else:
None
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)
result should be -->
{'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}}
字典不会自动组装自己并知道您想要的格式。由于您是从一个独立的列表开始的,因此您可以^{} 将它们分组,以便轻松地对它们进行迭代并构建字典:
结果
以下是一个可能的解决方案:
小心!所有列表的大小必须相同
相关问题 更多 >
编程相关推荐