这将是一个非常基本的东西,但我忘记了如何做到这一点。我只想删除列表中每个字符串的最后一行,如果它以':'结尾。我有
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.']
答案 0 :(得分:2)
不是非常强大,但希望能让你朝着正确的方向前进:
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.']
>
答案 1 :(得分:-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])
}
}