【问题标题】:Python dictionary valuesPython 字典值
【发布时间】:2013-03-06 20:38:46
【问题描述】:

您好,我刚刚开始学习如何编程并拥有一个我需要用 Python 编写的函数,这就是它背后的想法:

如果wordwordList 中并且完全由手中的字母组成,则返回True。否则,返回False。不会改变 hand 或 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

基本上,我的下一步是试图弄清楚如何将 word 与具有修正值的 handC 进行比较,并折扣任何值为 0 的键。 我认为(希望)这会奏效。

【问题讨论】:

  • 请发布您目前拥有的代码。
  • 如果你觉得你没有清楚地解释你的问题,发布你想出的代码通常很有帮助,因为它通常会清楚你的意图是什么以及您遇到麻烦的地方。它还可以帮助人们看到您至少尝试过一些东西。

标签: python dictionary


【解决方案1】:

如果没有您的代码,让我看看我是否理解您想要的:您正在尝试查看给定单词是否可以使用 hand 中的字母拼写,就好像用户对 @ 中的每个字母都有一个拼字游戏磁贴一样987654322@,是吗?

我个人只是复制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

【讨论】:

  • 只需将 hand.get 更改为 hand_cp.get 并将 hand[letter] 更改为 hand_cp[letter] 即可!非常感谢,非常感谢我在过去的几个小时里一直把头撞在墙上。
  • @PadraicCunningham 哎呀!很高兴我能提供帮助(并为后代修复)。
  • @Henry,我如何标记为已解决或这里的程序是什么?
  • @PadraicCunningham 答案左侧应该有一个复选标记(靠近上/下箭头);如果你点击它,它会变成绿色并将答案标记为“已接受”。
【解决方案2】:

这样的事情怎么样:

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

由于字符串是可迭代的,您可以逐个字符地检查。

祝你好运

【讨论】:

  • 字母重复的单词测试失败,这是我尝试的第一件事,python基本上看到abc等于abcc。感谢您的尝试!
  • @Padraic 很抱歉听到这个消息,我不明白你的意思。然而,在我的辩护中,它确实回答了这个定义:如果 word 在 wordList 中并且完全由手中的字母组成,它返回 True。否则,返回 False。不会改变 hand 或 wordList。 也许您可以更清楚地知道您需要它做什么?
【解决方案3】:

python Counter 类是你的朋友。你可以在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

【讨论】:

  • 谢谢你,很有趣,我想我需要多了解一下Counter!!
【解决方案4】:

这是我的

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 2017-07-18
    • 2013-11-15
    • 1970-01-01
    相关资源
    最近更新 更多