在n的text6中,第一个字母是大写的,所有其他字母都是小写的

2024-04-26 18:49:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python为nltk做一些在线实践。在

任务是从全套Text6中筛选出第一个字母大写,所有其他字母小写的单词。 打印出现的字数。在

有人能帮忙告诉确切的答案(因为这是nltk的标准文本)和代码中的错误。在

我试过以下代码:

from nltk.book import text6
import re
pattern = '[A-Z]+[a-z]+$'
capsword= [word for word in set(text6) if re.search(pattern, word)]
print(len(capsword))

我的实际产量是461。 但,我不确定预期产出,因为这是隐藏的。在


Tags: 答案代码importre字母单词wordpattern
2条回答

我改变了模式(包括像ABC这样的特殊字符词!或者ABC)而且它起作用了:

from nltk.book import text6
import re
pattern = '[A-Z][a-z*]'
a = [word for word in set(text6) if (re.search(pattern, word))]
print(len(a))

这应该对你有用,对我的壁画也有用

from nltk.book import text6    
title_words = [word for word in text6 if word.istitle()]

print(len(title_words))

Output: 2672

根据挑战,我们应该在文本6中使用完整的单词集。在

istitle()方法返回True对于第一个字母为大写字母、所有其他字母为小写的单词

相关问题 更多 >