简体   繁体   中英

Finding most common letter in a word list

I am writing a code for the game hangman. I am stuck in the part where I want to help the user to get a hint.

I need to create a function that accepts a word list , and the current pattern (for example:"___e_") and returns the most common letter in the word list.

I know I need to count the letters in each word and then return the maximum of that letter list but I don't quite know how to actually perform this.

I started by writing this code:

def choose_letter(words, pattern):
    new_list = [0] * 26
    for i in range(0, 26):
        for n in range(0, len(num_list)):
            if i == num_list[n]:
                new_list[i] += 1

    return new_list

but I get stuck because I don't know how to actually compare it to a letter.

I would love to hear some advice or guidance that will help me proceed.

*we didnt learn about dictionary yet

you can also use Counter from collections

from collections import Counter
Counter('abracadabra').most_common(3)


output: 
[('a', 5), ('r', 2), ('b', 2)]

You can use a combination of count and set to run the most frequent letter that is not contained in pattern , see below. The first line counts the number of occurrence for each letter, the second statement returns (one of) the most frequent letter(s).

def choose_letter(words, pattern):
    frequencies = [(letter, words.count(letter)) for letter in set(words) if letter not in pattern and letter!=' ']
    most_frequent_letter = max(frequencies, key=lambda x: x[1])[0]
    return most_frequent_letter


word = 'hangman is great fun'
pattern = '____a_____'
print choose_letter(word,pattern)

您可以使用字符串的count()方法来计算字符串中char的出现: 计算字符串中字符的出现

The obvious answer is to use a dictionary with the letters as keys. You can also use ord(char) to get an integer from a character, and use that integer as index for your list. Because your list is length 26 and the index for letters start at 97 (for lowercase 'a'), you can do something like:

def choose_letter(words, pattern): 
    new_list = [0] * 26
    for word in words:
        word = word.lower()
        for letter in word:
            index = ord(letter) -97
            new_list[index] += 1
    return new_list

To get the count for any given letter on the list you can:

print(new_list[ord(letter)])

To get the most common letter (note that if multiple letters have the highest value, only the first one will be returned):

chr(new_list.index(max(new_list)))

You can try:

>>> data = "Harsha Biyani"
>>> d = {}
>>> letters = set(data)
>>> for letter in letters :
    d[letter] = data.count(letter)


>>> key, value = max(d.items(), key=lambda x:x[1])  #in python 3
>>> key, value = max(d.iteritems(), key=lambda x:x[1])  # in python 2
>>> key
'a'
>>> value
3
>>> d
{'y': 1, 'H': 1, 'h': 1, ' ': 1, 'n': 1, 'B': 1, 's': 1, 'a': 3, 'i': 2, 'r': 1}

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