简体   繁体   中英

Find how many words start with certain letter in a list

I am trying to output the total of how many words start with a letter 'a' in a list from a separate text file. I'm looking for an output such as this.

35 words start with a letter 'a'.

However, i'm outputting all the words that start with an 'a' instead of the total with my current code. Should I be using something other than a for loop?

So far, this is what I have attempted:

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

The output I'm getting thus far:

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

and so on.

You could replace this with a sum call in which you feed 1 for every word in wordList that starts with a :

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

This can be further trimmed down if you use the boolean values returned by startswith instead, since True is treated as 1 in these contexts the effect is the same:

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

With your current approach, you're not summing anything, you're simply printing any word that matches. In addition, you're re-naming a_word from an int to the contents of the list as you iterate through it.

Also, instead of using a_word[0] to check for the first character, you could use startswith(character) which has the same effect and is a bit more readable.

You are using the a_words as the value of the word in each iteration and missing a counter. If we change the for loop to have words as the value and reserved a_words for the counter, we can increment the counter each time the criteria is passed. You could change a_words to wordCount or something generic to make it more portable and friendly for other letters.

a_words = 0

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

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

sum(generator) is a way to go, but for completeness sake, you may want to do it with list comprehension (maybe if it's slightly more readable or you want to do something with words starting with a etc.).

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

After that you may use len built-in to retrieve length of your new list.

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

Simple alternative solution using re.findall function(without splitting text and for loop):

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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