如何在python字典中存储和检索多行值
我想在python中使用以下类型的字典如何在python字典中存储和检索多行值,python,Python,我想在python中使用以下类型的字典 dictionary = { "Who is Elon Musk": "Elon Reeve Musk FRS is an American business magnate, investor and engineer. He is the founder, CEO, and lead designer of SpaceX; co-founder, CEO, and product architect of Tesla, Inc.;
dictionary = {
"Who is Elon Musk":
"Elon Reeve Musk FRS is an American business magnate, investor and engineer. He is the founder, CEO, and lead designer of SpaceX; co-founder, CEO, and product architect of Tesla, Inc.; and co-founder and CEO of Neuralink " ,
"Second Question":
"Second answer"
}
我想使用上述方法从问题中检索答案。这种方法有什么错?正确的方法是什么。还有比使用字典更好的方法来执行上述任务。您可以使用换行符
“\n”
来显示换行符应该出现的位置。连续的字符串文本被解释为单个字符串,因此我们可以轻松地在最后一个字符串将被打断的相同位置打断该文本
dictionary = {
"Who is Elon Musk":
"Elon Reeve Musk FRS is an American business magnate, investor and engineer.\n"
"He is the founder, CEO, and lead designer of SpaceX;\n"
"co-founder, CEO, and product architect of Tesla, Inc.;\n"
"and co-founder and CEO of Neuralink." ,
"Second Question":
"Second answer"
}
三引号字符串可以在这里工作,但是它们不能很好地使用Python的缩进规则,这使得代码很难阅读。字典通常用于可以作为键值对结构的数据。在这种情况下,它假设您知道确切的问题,以便查找答案(这真的是个好主意吗?)。用一套怎么样?它还可以防止重复你可以考虑使用2D表,其中一列/行是问题,一个是答案。它可能会快一点,并且会保持顺序。使用三重引号。参考python中的多行字符串,它在SO中已经有很多答案。多行字符串与单行字符串没有什么不同。你能解释为什么三重引号不起作用吗<代码>打印ing它应该按照存储方式为您提供多行段落。由于_repr____________)的实现方式不同,它在命令行上的外观也会有所不同strings@AdityaAryan请解释为什么三重引号不起作用。提供一个MCVE,否则这是一个关于多行字符串的问题,这个问题已经得到了回答。@bluesmank三重引号正在起作用。正如公认的答案中指出的那样,我对缩进有一些问题。但这个问题不应该被标记为重复,因为它会询问关于给定场景的其他方法的建议。