How to count the letters in a word? [duplicate]

牧云@^-^@ 提交于 2019-12-17 21:12:46

问题


I am trying to make a Python script which counts the amount of letters in a randomly chosen word for my Hangman game.

I already looked around on the web, but most thing I could find was count specific letters in a word. After more looking around I ended up with this, which does not work for some reason. If someone could point out the errors, that'd be greatly appreciated.

wordList = ["Tree", "Fish", "Monkey"]
wordChosen = random.choice(wordList)
wordCounter = wordChosen.lower().count['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
print(wordCounter)

回答1:


First off, your code contains an error that is rather important to understand:

wordChosen.lower().count['a', 'b'] #...

count is a function and so it requires you to surround its parameters with parentheses and not square brackets!

Next you should try to refer to Python Documentation when using a function for the first time. That should help you understand why your approach will not work.

Now to address your problem. If you want to count the number of letters in your string use len(wordChosen) which counts the total number of characters in the string.

If you want to count the frequencies of each letter a few methods have already been suggested. Here is one more using a dictionary:

import string
LetterFreq={}
for letter in string.ascii_lowercase:
    LetterFreq[letter] = 0
for letter in wordChosen.lower():
    LetterFreq[letter] += 1

This has the nice perk of defaulting all letters not present in the word to a frequency of 0 :)

Hope this helps!




回答2:


Are you looking for collections.Counter?

>>> import collections
>>> print(collections.Counter("Monkey"))
Counter({'M': 1, 'y': 1, 'k': 1, 'o': 1, 'n': 1, 'e': 1})
>>> print(collections.Counter("Tree"))
Counter({'e': 2, 'T': 1, 'r': 1})

>>> c = collections.Counter("Tree")
>>> print("The word 'Tree' has {} distinct letters".format(len(c)))
The word 'Tree' has 3 distinct letters
>>> print("The word 'Tree' has {} instances of the letter 'e'".format(c['e']))
The word 'Tree' has 2 instances of the letter 'e'



回答3:


Problem:
The count method only takes in one argument and you are trying to pass a whole list.

Solution:
Simply iterate over all the letters, then test if they are in the string before you print them and their amount.

import random
wordList = ["Tree", "Fish", "Monkey"]
wordChosen = random.choice(wordList)

letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

for letter in letters:
    if letter in wordChosen.lower():
        amount = str(wordChosen.lower().count(letter))
        print(letter + " : " + amount)

Result:
If the random word chosen is "Tree":

e : 2
r : 1
t : 1

Conclusion:
Using collections is definitely a more effective method, but I believe the way I have shown above creates more of the output you were looking for.



来源:https://stackoverflow.com/questions/32187934/how-to-count-the-letters-in-a-word

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!