查找列表中以某个字母开头的单词数

时间:2016-09-14 19:09:10

标签: python list python-3.x

我正在尝试从单独的文本文件中输出列表中以字母'a'开头的单词总数。我正在寻找这样的输出。

35 words start with a letter 'a'.

但是,我输出所有以'a'开头的单词而不是当前代码的总数。我应该使用for循环以外的东西吗?

到目前为止,这是我的尝试:

wordsFile = open("words.txt", 'r')
words = wordsFile.read()
wordsFile.close()
wordList = words.split()

print("Words:",len(wordList)) # prints number of words in the file.

a_words = 0

for a_words in wordList:
    if a_words[0]=='a':
        print(a_words, "start with the letter 'a'.")

到目前为止我得到的输出:

Words: 334
abate start with the letter 'a'.
aberrant start with the letter 'a'.
abeyance start with the letter 'a'.

等等。

4 个答案:

答案 0 :(得分:3)

您可以将sum来电替换为1,以wordList开头a中的每个字:

print(sum(1 for w in wordList if w.startswith('a')), 'start with the letter "a"')

如果您使用startswith返回的布尔值,则可以进一步减少此值,因为True在这些上下文中被视为1,效果是相同的:

print(sum(w.startswith('a') for w in a), 'start with the letter "a"')

使用您当前的方法,您不是在总结任何东西,而只是打印任何匹配的单词。此外,您在迭代时将a_wordint重新命名为列表内容。

此外,您可以使用具有相同效果且更具可读性的a_word[0],而不是使用startswith(character)来检查第一个字符。

答案 1 :(得分:2)

您在每次迭代中使用a_words作为单词的值并且缺少计数器。如果我们将for循环更改为words作为值并为计数器保留a_words,我们可以在每次传递条件时递增计数器。您可以将a_words更改为wordCount或其他通用内容,以使其更便于携带且对其他字母更友好。

a_words = 0

for words in wordList:
    if words[0]=='a':
        a_words += 1

print(a_words, "start with the letter 'a'.")

答案 2 :(得分:1)

sum(generator)是一种方法,但为了完整起见,你可能想要用列表理解来做(也许如果它稍微更具可读性,或者你想要用以<开头的单词做某事) em> a 等。)。

words_starting_with_a = [word for word in word_list if word.startswith('a')]

之后,您可以使用内置的len来检索新列表的长度。

print(len(words_starting_with_a), "words start with a letter 'a'")

答案 3 :(得分:0)

使用re.findall函数的简单替代解决方案(不拆分文本和for循环):

import re
...
words = wordsFile.read()
...
total = len(re.findall(r'\ba\w+?\b', words))
print('Total number of words that start with a letter "a" : ', total)
相关问题