如何计算句子中的单词数量,而忽略数字,标点符号和空格? [英] How to count the number of words in a sentence, ignoring numbers, punctuation and whitespace?
问题描述
我该如何计算句子中的单词数?我正在使用Python.
How would I go about counting the words in a sentence? I'm using Python.
例如,我可能具有字符串:
For example, I might have the string:
string = "I am having a very nice 23!@$ day. "
那将是7个字.我在每个单词之后/之前以及涉及数字或符号时的随机空格有麻烦.
That would be 7 words. I'm having trouble with the random amount of spaces after/before each word as well as when numbers or symbols are involved.
推荐答案
str.split()
不带任何参数的字符会在运行空白字符时分裂:
str.split()
without any arguments splits on runs of whitespace characters:
>>> s = 'I am having a very nice day.'
>>>
>>> len(s.split())
7
从链接的文档中:
如果未指定 sep 或
None
,则应用不同的拆分算法:连续空格的运行被视为单个分隔符,并且结果开头将不包含空字符串或如果字符串具有前导或尾随空格,则结束.
If sep is not specified or is
None
, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.
这篇关于如何计算句子中的单词数量,而忽略数字,标点符号和空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!