【发布时间】:2025-12-16 10:50:02
【问题描述】:
问题陈述:从完整的 text6 集中过滤那些单词,第一个字母大写,所有其他字母小写。将结果存储在变量 title_words 中。打印 title_words 中存在的单词数。
我已经尝试了所有可能的方法来找到答案,但不知道我在哪里落后。
import nltk
from nltk.book import text6
title_words = 0
for item in set(text6):
if item[0].isupper() and item[1:].islower():
title_words += 1
print(title_words)
我也试过这种方式:
title_words = 0
for item in text6:
if item[0].isupper() and item[1:].islower():
title_words += 1
print(title_words)
我不确定它需要多少计数,无论计数如何,它都不允许我通过挑战。如果我在这段代码中做错了什么,请告诉我
【问题讨论】:
-
你试过打印出
set(text6)中的一些items吗?如何打印其中的一些以及您的if条件是真还是假。 Control+C 并手动查看它,看看它的行为是否符合您的预期。 (那些只是标点符号的词呢?) -
@khelwood, :( 我试图将这些项目附加到列表中并手动检查列表但它不起作用。我的代码有什么问题还是我遗漏了一些重要的东西?
标签: python python-3.x nlp