首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在python字典中存储和检索多行值

如何在python字典中存储和检索多行值
EN

Stack Overflow用户
提问于 2018-06-30 02:20:03
回答 2查看 938关注 0票数 1

我想在以下类型的python中使用字典

代码语言:javascript
复制
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"  
}

我想使用上面的方法从问题中检索答案。

这种方法的错误之处以及正确的方法是什么。此外,还有比使用字典更好的方法来执行上述任务。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-30 02:52:14

您可以使用换行符"\n"来显示应该在何处换行。连续的字符串文字被解释为单个字符串,因此我们可以很容易地在最后一个字符串将被断开的相同位置中断文字。

代码语言:javascript
复制
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的缩进规则,使得代码难以阅读。

票数 2
EN

Stack Overflow用户

发布于 2018-06-30 04:26:21

字典通常用于可以被构造为键-值对的数据。在这种情况下,它假设您知道确切的问题,以便查找答案(这真的是一个好主意吗?)。使用set怎么样?它还可以防止重复。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51107411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档