繁体   English   中英

Python:使用.isalpha() 计算字数中的特定单词/字符

[英]Python: Using .isalpha() to count specific words/characters in a word count

我创建了一个 function 可以计算文本文件中的特定单词或字符。

但是我想创建一个条件,其中 function 只计算一个被字母包围的字符。 例如在文本文件中。

'This test is an example, this text doesn't have any meaning. It is only an example.'

如果我要通过我的 function 运行此文本,测试撇号 (') 的计数,它将返回 3。但是我希望它返回 1,仅适用于 2 个字母字符内的撇号(例如不是或不会),但我希望它忽略没有被字母包围的所有其他撇号,例如单引号。

我尝试使用 .isalpha() 方法,但语法有问题。

我认为正则表达式会更好,但如果你必须使用isalpha ,比如:

s = "'This test is an example, this text doesn't have any meaning. It is only an example.'"
sum(s[i-1].isalpha() and s[i]=="'" and s[i+1].isalpha() for i in range(1,len(s)-1))

返回 1。

如果您只想打折包含字符串本身的引号,最简单的方法可能是在计数之前将它们从字符串中strip

>>> text = "'This test is an example, this text doesn't have any meaning. It is only an example.'"
>>> text.strip("'").count("'")
1

另一种方法是使用像\w'\w这样的正则表达式,即字母,后跟' ,然后是字母:

>>> sum(1 for _ in re.finditer("\w'\w", text))
1

这也适用于字符串内的引号:

>>> text = "Text that has a 'quote' in it."
>>> sum(1 for _ in re.finditer("\w'\w", text))
0

但它也会错过后面没有另一个字母的撇号:

>>> text = "All the houses' windows were broken."
>>> sum(1 for _ in re.finditer("\w'\w", text))
0

正如 xnx 已经指出的,正确的方法是使用正则表达式:

import re

text = "'This test is an example, this text doesn't have any meaning. It is only an example.'"

print(len(re.findall("[a-zA-Z]'[a-zA-Z]", text)))
"""
Out:
    1
"""

这里模式中的撇号被一组英文字母包围,但是有许多预定义的字符集,有关详细信息,请参阅RE 文档

你应该只使用正则表达式:

import re

text = "'This test is an example, this text doesn't have any meaning. It is only an example.'"

wordWrappedApos = re.compile(r"\w'\w")
found = re.findall(wordWrappedApos, text)
print(found)
print(len(found))

如果要确保其中没有数字,请用“\w”替换“[A-Za-z]”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM