Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检查值是否同时出现在字符串和字典中_Python_String_Dictionary_Key - Fatal编程技术网

Python 检查值是否同时出现在字符串和字典中

Python 检查值是否同时出现在字符串和字典中,python,string,dictionary,key,Python,String,Dictionary,Key,作为一个更大问题的一部分,我必须编写一个函数,检查给定单词中的所有字母是否出现在名为hand的字典中(也出现在单词列表中,但我在这里省略了该部分),并确保如果一个字母出现两次,例如,在单词中,它必须至少在字典中出现相同的次数。以下是我编写的函数之一: def is_valid_word(word, hand): for let in word: if let in hand.keys(): if hand[let]>=word.count(l

作为一个更大问题的一部分,我必须编写一个函数,检查给定单词中的所有字母是否出现在名为hand的字典中(也出现在单词列表中,但我在这里省略了该部分),并确保如果一个字母出现两次,例如,在单词中,它必须至少在字典中出现相同的次数。以下是我编写的函数之一:

def is_valid_word(word, hand):
    for let in word:
        if let in hand.keys():
            if hand[let]>=word.count(let):
                return True
            else:
                return False
我也试过这样做:

def is_valid_word(word, hand):
    for let in word:
        if let not in hand.keys():
            return False
        else:                                 #in another variation I merged 
            if hand[let]>=word.count(let):    # these two lines with _elif_
                return True
            else:
                return False
在其他类似的函数中,我没有专门编写hand.keys(),只是使用了

if let in/not in hand 
但每次我尝试使用

print is_valid_word("account", {"a":1, "c":1, "l":2, "n":1, "o":3, "r":2, "t":1, "y":1})
即使字母“c”在单词中出现了两次,但在字典中只出现了一次,它也会返回真值(我也在不同的字典中尝试了单词“breash”,但我在这里给出的第二个示例与其他示例不同,它是按预期的方式处理的)。 你知道怎么做吗

编辑:这是问题中的解释方式,希望能让问题更容易理解:

“单词表中有一个有效的单词,它完全由当前手上的字母组成。 实现is_valid_word函数

def is_valid_word(word, hand, word_list):
"""Returns True if word is in the word_list and is entirely 
composed of letters in the hand. Otherwise, returns False.
Does not mutate hand or word_list.
word: string
hand: dictionary (string -> int)
word_list: list (string)
"""
# TO DO ... "

函数返回word中第一个字母的结果,这将终止函数。的
循环不会超过第一个字母执行


修改函数,使其仅在所有字母都有效时返回
True
for
循环不会在第一个无效字母之后执行。

函数返回word中第一个字母的结果,从而终止函数。
for
循环不会在第一个字母之后执行


修改该函数,使其仅在所有字母都有效时返回
True
。(对于第一个无效字母,仍然可以返回
False
。)如果我没有误解,您希望检查单词的所有字母是否都是dict的键,并且dict中的相关值是否大于单词中字母的出现时间。不幸的是,您的程序有一个明显的错误。它仅在检查第一个字母后才终止循环

修改后应如下所示:

def is_valid_word(word, hand):
    for let in word:
        if let in hand and hand[let]>=word.count(let):
            continue
        else:
            return False
    return True

我希望这会有所帮助。

如果我没有误解,您希望检查单词的所有字母是否都是dict的键,并且dict中的相关值是否大于单词中字母的出现时间。不幸的是,您的程序有一个明显的错误。它仅在检查第一个字母后才终止循环

修改后应如下所示:

def is_valid_word(word, hand):
    for let in word:
        if let in hand and hand[let]>=word.count(let):
            continue
        else:
            return False
    return True

我希望它能帮上忙。

我会使用
所有的
集合。计数器
使它更直观

>>> def is_valid_word(word, hand, word_list):
...     if word in word_list:
...         return all(letter_dict[letter] >= hand[letter] for letter in Counter(word) if letter in hand ) 
>>>     return False

我会使用
all
collections.Counter
使它更直观

>>> def is_valid_word(word, hand, word_list):
...     if word in word_list:
...         return all(letter_dict[letter] >= hand[letter] for letter in Counter(word) if letter in hand ) 
>>>     return False

问题不是循环机制;问题是在检查第一个字母后总是
返回True
返回False
,这会终止函数。明白了。我会看看我能做些什么问题不是循环机制;问题是在检查后总是
返回True
返回False
第一个字母,它终止了函数。明白了。我会看看我能做些什么。我还没有研究这些概念,但会马上检查出来。谢谢!我还没有研究这些概念,但会马上检查出来。谢谢!