2016-06-28 5 views
-2

中の例外を除いて、文字列の各単語の最初の文字を大文字にis "、" it "、" if "など?これは私が与えられたものであるPythonの

+0

あなたは何をしようとしたのですか?具体的な問題は何ですか? (c) –

+0

あなたは長さが4以上であり、res {0:1} .upper()を使用することができます。 – dmitryro

答えて

3

だけ大文字にするべきではない言葉フィルタリング:ここでは、不要な一時変数を削除し、受け入れる

def sensible_title_caps(str, no_caps_list = ["and", "is", "it", "if"]): 
    words = [] 
    for word in str.split(): 
     if word not in no_caps_list: 
      word = word.capitalize() 
     words.append(word) 
    return " ".join(words) 

:機能のクリーンバージョンのように書くことができ

no_caps_list = ["and", "is", "it", "if"] 
def cap(s):  
    lst = s.split() 
    res = ''  
    for word in lst: 
     if word not in no_caps_list: 
      word = word.capitalize() 
     res = res + ' '+ word 
    return res 

を有意義なデフォルト値を持つパラメータ(no_caps_list)として大文字にしない単語のリスト。リストとして難読化

それとも恐ろしいは理解:

def obfuscated_caps(str, no_caps_list = ["and", "is", "it", "if"]): 
    return " ".join([w in no_caps_list and w or w.title() for w in str.split()]) 
+0

これらは無駄な変数ではありませんし、短いリスト内のコードを難読化しても、問題の理解 –

+0

[OK]を、よりクリーンな代替バージョンがありますが、私はリストの理解を行うことは、それがちょうどひどい難読化されるクリーナーになる方法を見ていない。 –

+0

はい。私はこの長いことを考えて、リストの理解は*読むのが少し難しいでしょう。私は満足しています;) – zondo

関連する問題