我想数数“句号”,例如句号、感叹号和问号。在
我已经写了一个小循环来完成这个,但是我想知道是否有更好的方法。不允许使用内置函数。在
for line in textContent:
numberOfFullStops += line.count(".")
numberOfQuestionMarks += line.count("?")
numberOfQuestionMarks += line.count("!")
numberOfSentences = numberOfFullStops + numberOfQuestionMarks + numberOfExclamationMarks
假设您想计算一个句子中的终端标点符号,我们可以通过循环每个字符串的字符并过滤标点来生成(character,count)对的字典。在
演示
以下是三种自上而下的中间到初级数据结构选项:
为了进一步扩展,对于一个单独的
^{pr2}$sentences
的列表,循环使用后一个选项。在注:要求出每个句子的标点总数,请加上
dict.values()
,例如sum(cd.values())
。在更新:假设您要通过词尾点选来拆分句子,请使用正则表达式:
注意
line
有5个词尾,但只有3个句子。因此regex是一种更可靠的方法。在参考文献
相关问题 更多 >
编程相关推荐