2013-03-06 146 views
0

嗨,我剛開始學習如何編程,並有我需要用Python編寫一個函數,這是它背後的想法:Python字典值

它返回True如果word是在wordList是完全由手中的字母組成。否則,返回False。不改變手或wordList。

有一個函數調用,檢查用戶想出的單詞中的字母的頻率,這是一個轉換爲字典,我嘗試過使用iteritems各種方式,但無濟於事,我陷入了困境對於重複字母的單詞,如果用戶手中沒有該字母的兩個條目,它們會被返回爲真。

對不起,如果這不清楚,我只在兩個星期前開始。 任何指針會很棒我一直在這個很長一段時間卡住!

def isValidWord(hand,word,wordList): 

    """ 
    Returns True if word is in the wordList and is entirely 

    composed of letters in the hand. Otherwise, returns False. 

    Does not mutate hand or wordList. 

    word: string 
    hand: dictionary (string -> int) 
    wordList: list of lowercase strings 
    """ 

    wordC = getFrequencyDict(word) 
    handC = dict.copy(hand) 
    if word not in wordList: 
     return False 
    for c in word: 
     if c not in hand: 
      return False 
     for k,v in wordC.iteritems(): 
      if k in hand and v > 1: 
       handC[k] -= 1 

基本上我的下一個步驟,試圖找出如何字比較handC與修正值和貼現與零值的任意鍵。 我認爲(希望)會起作用。

+4

請張貼你到目前爲止的代碼。 – thegrinner 2013-03-06 20:42:07

+1

如果您覺得自己沒有清楚地解釋您的問題,那麼發佈您提供的代碼通常會非常有幫助,因爲它通常會清理您的意圖以及您遇到問題的位置。它還可以幫助人們看到你至少嘗試了一些東西。 – Mariano 2013-03-06 20:55:27

回答

1

沒有你的代碼,讓我看看,如果我理解你想要什麼:你想看看如果給定的話可以使用字母hand被拼寫爲,如果用戶有一個拼字遊戲瓷磚對於每個字母hand,是嗎?

就我個人而言,我只是複製hand字典,然後允許更改副本。類似這樣的:

def is_valid_word(hand, word, wordlist): 
    hand_cp = dict(hand) 
    for letter in word: 
     if hand_cp.get(letter): 
      # The letter is in our hand, so "use it up". 
      hand_cp[letter] = hand_cp[letter] - 1 
     else: 
      # The letter isn't in our hand, so the word isn't valid. 
      return False 

    # If we can make the word, now make sure it's a real word: 
    # (If wordlist is long, you might want to sort it and do a real search) 
    if word not in wordlist: 
     return False 

    # We haven't found any reason to return False, so this is a valid word. 
    return True 
+1

只需將hand.get改爲hand_cp.get並將[letter]改爲hand_cp [letter]即可,它的工作原理完美無缺!!!非常感謝,非常感謝我敲我的頭撞在牆上的最後幾個小時。 – 2013-03-06 21:40:36

+0

@PadraicCunningham哎呦!很高興我可以幫助(併爲後人修復)。 – 2013-03-06 21:43:53

+0

@亨利,我要怎樣爲解決或此程序是什麼? – 2013-03-06 21:45:04

1

怎麼是這樣的:

def isValidWord(hand, word, word_list): 
    if word not in word_list: 
     return False 
    for c in word: 
     if c not in hand: 
      return False 
    return True 

由於字符串是可迭代的,你可以通過字符校驗字符。

好運

+0

重複字母的單詞測試失敗,那是我第一次嘗試,python基本上看到abc等於abcc.Thanks for trying! – 2013-03-06 21:23:46

+0

@Padraic對不起,我不明白你的意思。然而,在我的辯護中,它確實回答了這個定義:如果word是在wordList中並且完全由手中的字母組成,它將返回True。否則,返回False。不要改變手或wordList._也許你可以更清楚你需要做什麼? – Mariano 2013-03-06 21:27:30

0

python Counter class是你的朋友。您可以在python 2.7 and later做到這一點:

from collections import Counter 

def is_valid_word(hand, word, word_list): 
    letter_leftover = Counter(hand) 
    letter_leftover.subtract(Counter(word)) 
    return word in word_list and all(v >= 0 for v in letter_leftover.values()) 

然後:

>>> def test(): 
...  hand = "traipse" 
...  word_list = ["all", "the", "words", "in", "English", 
        "parts", "pines", "partiers"] 
...  print is_valid_word(hand, "parts", word_list) 
...  print is_valid_word(hand, "pines", word_list) 
...  print is_valid_word(hand, "partiers", word_list) 
... 
>>> test() 
True 
False 
False 
+0

謝謝你,很有意思,我想我需要學習更多的關於反! – 2013-03-06 23:03:03

0

這裏是我的

def isValidWord(word, hand, wordList): 
    """ 
    Returns True if word is in the wordList and is entirely 
    composed of letters in the hand. Otherwise, returns False. 

    Does not mutate hand or wordList. 

    word: string 
    hand: dictionary (string -> int) 
    wordList: list of lowercase strings 
    """ 
    if word in wordList: 
     if set(word).issubset(set(hand)): 
      return True 
     else: 
      return False 
    else: 
     return False