2017-06-27 27 views
-1

正如您可以想象的,我正在開始使用Python(對於noob問題抱歉)。我正在編寫一個使用一些長字典條目的代碼,並且我正努力讓Python滿意80個字符的限制。如何在Python字典中正確地格式化長字符串

這裏是我的字典的剪斷:

"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"}, 

那麼,什麼是對付在字典中的極長串的正確方法是什麼?

Thanks guys guys

+0

是你的老闆抱怨,或者你的棉短絨? –

回答

3

Python沒有「80個字符的限制」。這是一種風格建議,但你可以自由不尊重它。但是,它可能會讓你的代碼難以閱讀,而你的編輯可能會警告你這些長長的行。要解決這個問題

一種方法是使用automatic string concatenation

多個相鄰的字符串或(由空格分隔), 可能使用不同的引用慣例,允許字節的文字,他們的 意義是與它們的連接相同。因此,「你好」「世界」是 相當於「helloworld」。

所以,你可以寫你的字典爲:

{ 
    "B1LYP": "The one - parameter hybrid functional with Becke '88" 
      " exchange and Lee - Yang - Parr correlation(25 % HF " 
      "exchange)", 
    ... 
+0

謝謝你的回答。 –

2

Python本身並不真正在乎;你會收到警告,而不是錯誤,因爲你沒有遵守正確的格式。如果您想修復它,請嘗試按Enter鍵分隔線條。

如果您有一個非常大的字典,請考慮將其放入一個文本文件並在需要時從中讀取。

+0

謝謝你的提示,我的朋友。 –