首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Python字典中正确格式化长字符串

如何在Python字典中正确格式化长字符串
EN

Stack Overflow用户
提问于 2017-06-27 21:35:15
回答 1查看 2.1K关注 0票数 0

正如您可以想象的那样,我正在开始使用Python (对于noob的问题很抱歉)。我正在编写一个使用一些长字典条目的代码,并且我很难让Python满意于80个字符的限制。

这是我字典里的一个片段:

代码语言:javascript
复制
"Hybrid functionals": {
    "B1LYP": "The one - parameter hybrid functional with Becke '88 exchange and Lee - Yang - Parr correlation(25 % HF exchange)",
    "B3LYP and B3LYP/G": "The popular B3LYP functional (20% HF exchange) as defined in the TurboMole program system and the Gaussian program system, respectively",
    "O3LYP": "The Handy hybrid functional",
    "X3LYP": "The Xu and Goddard hybrid functional",
    "B1P": "The one-parameter hybrid version of BP86",
    "B3P": "The three-parameter hybrid version of BP86",
    "B3PW": "The three-parameter hybrid version of PW91",
    "PW1PW": "One-parameter hybrid version of PW91",
    "mPW1PW": "One-parameter hybrid version of mPWPW",
    "mPW1LYP": "One-parameter hybrid version of mPWLYP",
    "PBE0": "One-parameter hybrid version of PBE",
    "PW6B95": "Hybrid functional by Truhlar",
    "BHANDHLYP": "Half-and-half hybrid functional by Becke"},

那么,在字典中处理超长字符串的正确方法是什么呢?

谢谢各位

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-27 22:05:58

Python没有“80个字符限制”。这是一个时尚的建议,但你可以不尊重它。然而,它可能会使您的代码更难阅读,您的编辑器可能会警告您注意这些长行。

解决这个问题的一种方法是使用自动串串接

允许多个相邻的字符串或字节文本(用空格分隔),可能使用不同的引用约定,它们的含义与它们的连接相同。因此,"hello“'world‘相当于"helloworld”。

所以,你可以把你的字典写成:

代码语言:javascript
复制
{
    "B1LYP": "The one - parameter hybrid functional with Becke '88" 
             " exchange and Lee - Yang - Parr correlation(25 % HF " 
             "exchange)",
    ...
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44790327

复制
相关文章

相似问题

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