2

我正在创建一个马尔可夫链算法。我输出了一个名为 sentence 的变量,其中包含一串句子。我想把句子变成大小写,所以我写了这个:

for l in range(0, len(sentence)-1):
    if l == 0:
        sentence[l].upper()
    elif sentence[l] == ".":
        sentence[l+2].upper()

这样做的目的是将第一个单词的第一个字母大写。那么如果遇到句号,后面的两个字符就是一个新句子的开始。但是,我不知道如何改变句子。这是我尝试过的,但是是非法的:

elif sentence[l] == "."
    sentence[l+2] = sentence[l+2].upper()

不,sentence.title() 将不起作用,因为它会使每个单词的标题大小写。

4

2 回答 2

4

Python 已经有一个.capitalize()方法:

>>> 'this is a sentence.'.capitalize()
'This is a sentence.'

问题是,它不适用于多个句子:

>>> 'this is a sentence. this is another.'.capitalize()
'This is a sentence. this is another.'

它也不能很好地处理空格:

>>> ' test'.capitalize()
' test'
>>> 'test'.capitalize()
'Test'

为了解决这个问题,您可以拆分句子,去掉空格,将它们大写,然后将它们重新组合在一起:

>>> '. '.join([s.strip().capitalize() for s in 'this is a sentence. this is another.'.split('.')]).strip()
'This is a sentence. This is another.'

你也可以用正则表达式来做,它应该更通用一点:

import re

def capitalizer(match):
    return match.group(0).upper()

sentence = 'this is a sentence. isn\'t it a nice sentence? i think it is'
print re.sub(r'(?:^\s*|[.?]\s+)(\w)', capitalizer, sentence)

和输出:

This is a sentence. Isn't it a nice sentence? I think it is
于 2013-06-06T22:51:34.910 回答
1

在 Python 中,字符串是不可变的。您可以将新字符串再次分配给相同的变量,或者将其转换为列表,改变列表,然后''.join()再次。

>>> sentence = list("hello. who are you?")
>>> for l in range(0, len(sentence)-1):
...     if l == 0:
...         sentence[l] = sentence[l].upper()
...     elif sentence[l] == ".":
...         sentence[l+2] = sentence[l+2].upper()
...
>>> ''.join(sentence)
'Hello. Who are you?'
于 2013-06-06T22:48:54.680 回答