Python已经string.find()并且string.rfind()在字符串中获取子字符串的索引.
我想知道,也许有类似的东西string.find_all()可以返回所有已创建的索引(不仅从开始或从头到尾)?
例如:
string = "test test test test"
print string.find('test') # 0
print string.rfind('test') # 15
#this is the goal
print string.find_all('test') # [0,5,10,15]
Run Code Online (Sandbox Code Playgroud) 我有一个文本文件.我需要一个句子列表.
如何实施?有许多细微之处,例如在缩写中使用点.
我的旧正则表达式很糟糕.
re.compile('(\. |^|!|\?)([A-Z][^;?\.<>@\^&/\[\]]*(\.|!|\?) )',re.M)
Run Code Online (Sandbox Code Playgroud) 我需要解析Python中段落的句子.是否有现成的包,或者我应该尝试在这里使用正则表达式?
我的程序采用一个文本文件,并将每个句子分成一个列表,使用的split('.')意思是,当它注册一个完整的停止时它会分裂但是它可能是不准确的.
str='i love carpets. In fact i own 2.4 km of the stuff.'
Run Code Online (Sandbox Code Playgroud)
listOfSentences = ['i love carpets', 'in fact i own 2', '4 km of the stuff']
listOfSentences = ['i love carpets', 'in fact i own 2.4 km of the stuff']
Run Code Online (Sandbox Code Playgroud)
我的问题是:我如何分割句子的结尾,而不是每一个句号.