如何计算句子中的单词数,忽略数字、标点符号和空格? [英] How to count the number of words in a sentence, ignoring numbers, punctuation and whitespace?

查看:40
本文介绍了如何计算句子中的单词数,忽略数字、标点符号和空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何计算句子中的单词数?我正在使用 Python.

例如,我可能有这样的字符串:

string = "我度过了非常愉快的 23!@$ 一天."

那将是 7 个字.我在每个单词之后/之前以及涉及数字或符号时遇到随机数量的空格问题.

解决方案

str.split() 不带任何参数在空白字符运行时拆分:

<预><代码>>>>s = '我今天过得很愉快.'>>>>>>len(s.split())7

来自链接的文档:

<块引用>

如果 sep 未指定或 None,则应用不同的拆分算法:连续空白的运行被视为单个分隔符,结果将不包含如果字符串有前导或尾随空格,则在开头或结尾处为空字符串.

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. "

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() without any arguments splits on runs of whitespace characters:

>>> s = 'I am having a very nice day.'
>>> 
>>> len(s.split())
7

From the linked documentation:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆