簡體   English   中英

Python - 使用拼字游戲字母值字典對單詞列表進行評分

[英]Python - Scoring a list of words using a dictionary of Scrabble letter values

對不起,如果這一切都很簡單,我是編碼新手。

我得到了一個單詞列表,即

my_words = [ 'Apple", 'banana', 'Grape', 'orange' ]

我還得到了一個字母值字典,

letter_value = {'a':1 , 'b':3, 'c':3, 'd':2, 'e':1, 'f':4. 'g':2, 'h':4, 'i':1, 'j':8, 'k':5, 'l':1, 'm':3, 'n':1. 'o':1, 'p':3, 'q':10, 'r':1, 's':1, 't':1, 'u':1, 'v':8, 'w':4, 'x':8, 'y':4, 'z':10}

我必須

創建一個函數,它接受一個單詞並計算該單詞的拼字游戲分數,我這樣做了:

def scrabblescore(words): 
     total = 0
     for letter in word: 
         total += letter_value[letters]
     return total 

當我嘗試時, print(scrabblescore("myscore")它工作正常

我不知道該怎么做

編寫一個函數,該函數將獲取整個單詞列表,遍歷該列表,並使用第一個函數計算列表中每個單詞的得分並制作字典,例如

{'apple': 20, 'banana':10} etc.

提前謝謝大家

我不打算把完整的代碼放在這里(這是給你弄明白的),但我會告訴你你應該做什么。

1:設置字典變量。

2:遍歷值。 對於 my_words 中的每個單詞,請執行以下操作:

3:遍歷字母。 對於 my_words 中 word 中的每個字母,請執行以下操作:

4:在字典中查找字母的鍵值。 將其添加到臨時變量中。

5:設置word為key,臨時變量為value。

首先修復您的方法,關於變量的名稱,並使用.lower()處理大寫字母

def scrabble_score(word):
    total = 0
    for letter in word:
        total += letter_value[letter.lower()]
    return total

然后對於單詞列表,您可以使用 dict-comprehension 或經典的 for 循環

def scrabble_score_words(words):
    return {word: scrabble_score(word) for word in words}


def scrabble_score_words(words):
    result = {}
    for word in words:
        result[word] = scrabble_score(word)
    return result

這是一個 foreach 字符循環:

for element in string_name:
    

你可以讓它像:

def scrabble_score(word):
    total = 0
    for letter in word:
        # hit here
        # 1. added lower() to make the letter lowercase
        # 2. IMPORTANT use letter 
        total += letter_value[letter.lower()] 
    return total

def scrabble_score_list(words):
    """take a list of the words"""
    # Just a wrapper for the first function
    rd = {}
    for x in words:
        rd[x] = scrabble_score(x)
    return rd

我喜歡做家庭作業。 我求你學習; 不只是復制。

""" Yet another SO homework assignment """

letter_value = {'a':1, 'b':3, 'c':3, 'd':2, 'e':1, 'f':4, 'g':2, 'h':4, 'i':1,
                'j':8, 'k':5, 'l':1, 'm':3, 'n':1, 'o':1, 'p':3, 'q':10, 'r':1,
                's':1, 't':1, 'u':1, 'v':8, 'w':4, 'x':8, 'y':4, 'z':10}


def scrabble_score(word):
    """
    For a specific word, calculate the scrabble score of that word.
    :returns: The numeric scrabble score of a word.
    """
    total = 0
    for letter in word.lower():
        total += letter_value[letter]
    return total


def scrabble_dict(word_list):
    """
    A function that will take the whole list of words, iterate over the list,
    and use the scrabblescore() to compute the score of each word in the list
    and make a dictionary like {'Apple': 20, 'Banana':10}
    """
    word_dict = {}
    for word in word_list:
        score = scrabble_score(word)
        word_entry = {word: score}
        word_dict.update(word_entry)

    return word_dict


def main():
    """ Test out the scrabble dictionary """
    scrabble_dictionary = {}
    word_set_1 = ['Apple', 'banana', 'Grape', 'orange']
    scrabble_dictionary.update(scrabble_dict(word_set_1))

    word_set_2 = ['Orange', 'Pear', 'Grape', 'Pineapple', 'Xerox']
    scrabble_dictionary.update(scrabble_dict(word_set_2))

    print(scrabble_dictionary)


if __name__ == "__main__":
    main()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM