我想在以下类型的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.; and co-founder and CEO of Neuralink " ,
"Second Question":
"Second answer"
}
我想使用上面的方法从问题中检索答案。
这种方法的错误之处以及正确的方法是什么。此外,还有比使用字典更好的方法来执行上述任务。
发布于 2018-06-30 02:52:14
您可以使用换行符"\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的缩进规则,使得代码难以阅读。
发布于 2018-06-30 04:26:21
字典通常用于可以被构造为键-值对的数据。在这种情况下,它假设您知道确切的问题,以便查找答案(这真的是一个好主意吗?)。使用set怎么样?它还可以防止重复。
https://stackoverflow.com/questions/51107411
复制相似问题