Python字典值

Python字典值,python,dictionary,Python,Dictionary,大家好,我刚刚开始学习如何编程,以及如何使用Python编写一个函数,这就是它背后的思想: 如果word在wordList中并且完全由手中的字母组成,则返回True。否则,返回False。不会变异hand或wordList 有一个可以调用的函数,它可以检查用户想出的单词中字母的频率,并将其转换为dict,我尝试了各种方法使用iteritems,但没有效果,我被那些重复字母的单词卡住了,当用户手中没有该字母的两个条目时,它们将作为true返回 对不起,如果这不清楚,我两周前才开始。 任何指点都很好

大家好,我刚刚开始学习如何编程,以及如何使用Python编写一个函数,这就是它背后的思想:

如果
word
wordList
中并且完全由手中的字母组成,则返回
True
。否则,返回
False
。不会变异hand或wordList

有一个可以调用的函数,它可以检查用户想出的单词中字母的频率,并将其转换为dict,我尝试了各种方法使用iteritems,但没有效果,我被那些重复字母的单词卡住了,当用户手中没有该字母的两个条目时,它们将作为true返回

对不起,如果这不清楚,我两周前才开始。 任何指点都很好,我已经在这上面呆了很长时间了

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
基本上,我的下一步是尝试找出如何将word与具有修正值的handC进行比较,并对任何值为零的键进行折扣。
我想(希望)这会奏效。

像这样的东西怎么样:

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
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
由于字符串是可编辑的,所以可以逐个字符进行检查


祝你好运

如果没有你的代码,让我看看我是否理解你想要的:你试图看看给定的单词是否可以用
手上的字母拼写,就像用户在
手上的每个字母都有拼字块一样,是吗

就我个人而言,我只需复制
手动
词典,然后允许对副本进行更改。大概是这样的:

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
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

python
计数器
类是您的朋友。您可以通过以下方式执行此操作:

然后:

这是我的

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

请发布您目前掌握的代码。如果您觉得自己没有清楚地解释您的问题,发布您提出的代码通常会非常有帮助,因为它通常会澄清您的意图以及您遇到的问题。它还帮助人们看到你至少尝试过一些东西。重复字母的单词没有通过测试,这是我尝试的第一件事,python基本上看到abc等于abcc。谢谢你的尝试@听到这个消息我很难过,我不明白你的意思。然而,在我的辩护中,它确实回答了这个定义:如果单词在单词列表中并且完全由手上的字母组成,它将返回True。否则,返回False。不会变异hand或wordList。也许你可以更清楚地知道你需要它做什么?只是需要换手。动手,把[信]交到手上,它就完美地工作了!!!。非常感谢,非常感谢我在过去的几个小时里把头撞在墙上。@PadraicCunningham哇!很高兴我能帮上忙(并为子孙后代做好了准备)。@Henry,我该如何标记为已解决,或者这里的程序是什么?@PadraicCunningham答案左侧(上/下箭头附近)应该有一个复选标记;如果你点击它,它会变成绿色,并将答案标记为“已接受”。谢谢,非常有趣,我想我需要了解更多有关计数器的信息!!