2017-10-16 121 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) 

輸出

> python3 test.py 
['A sentence. Another sentence', 'One more sentence.', 'One last sentence.'] 
> 
相關問題