2017-10-16 13 views
0

これは本当に基本的なものになるだろうが、私はそれをどうやって行うのか忘れてしまった。 ":"で終わるリストの各文字列の最後の行を削除したいだけです。私は印刷し現時点で特定の文字で終了する場合、最後のSENTENCEを文字列で削除するには?

desc1 = ['A sentence. Another sentence', 'One more sentence. A sentence that finishes with:', 'One last sentence. This also finishes with a:'] 
for string in desc1: 
    if string.endswith(':'): 
     a = string.split('.') 
     b = a[:-1] 
     c = '.'.join(map(str, b)) 
     print (c) 

を持っている:

One more sentence 
One last sentence 

は、どのように私は今、それは次のように出力されるようにそれを得るのです:あなたは行く

['A sentence. Another sentence', 'One more sentence.', 'One last sentence.'] 

答えて

-1

ここで: -

var descFinal = []; 
for(var i=0; i< desc1.length; i++){ 
    if(desc1[i].endsWith(":")){ 
    descFinal.push(desc1[i].substring(0, desc1[i].lastIndexOf('.') + 1)); 
    }else{ 
    descFinal.push(desc1[i]) 
    } 
} 
+0

これ... javaのです。あなたはその質問を見ましたか? –

1

非常に堅牢ではありませんが、うまくいけばgら、あなたは正しい方向に向かって:

strings = ['A sentence. Another sentence', 'One more sentence. A sentence that finishes with:', 'One last sentence. This also finishes with a:'] 

new_strings = [] 

for string in strings: 
    if string.endswith(':'): 
       sentences = string.split('.') 
       string = '.'.join(sentences[:-1]) + '.' 

    new_strings.append(string) 

print(new_strings) 

OUTPUT

> python3 test.py 
['A sentence. Another sentence', 'One more sentence.', 'One last sentence.'] 
> 
関連する問題