別のテキストファイルのリストにある'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'.
と。
_Howeverは、私がためにそれはありますa'_」で始まるすべての単語を出力していますあなたの ' forループの場合、 'print'文があります。あなたが言葉を印刷したくない場合は、...しないでください。 –