Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.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_Conditional Statements_Logical Operators - Fatal编程技术网

&引用;或;Python中的条件

&引用;或;Python中的条件,python,conditional-statements,logical-operators,Python,Conditional Statements,Logical Operators,我想检查一个值是-1还是小于另一个值 为了做到这一点,我做了以下工作: 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.

我想检查一个值是-1还是小于另一个值

为了做到这一点,我做了以下工作:

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 not in wordList:
        return False
    for k, v in getFrequencyDict(word).items():
        if hand.get(k, -1) < v or hand.get(k, -1) == -1:
            return False
    return True

这可以用上面给定的方式写。(注意,这与你的问题不完全相同,只是想法)

如果你知道dict中的所有值都是正值,那么检查

if hand.get(k, -1) < v:
if hand.get(k,-1)
就够了


在更一般的情况下,这似乎是使用
的正确方法(您的代码清楚地表明您检查了一些东西,是否为默认值)。

使用
-1
作为默认值在这里似乎是多余的,并且不允许推广到
hand
包含负值的情况

if hand.get(k) is None or hand.get(k) < v:
    ...

只需使用设置此操作不需要词典Set支持差分操作

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: set of letters
          eg: set(["u", "s", "a"] or set("usa")
    wordList: set of strings
          eg: set(["zeus", "osiris", "thor"])
    """
    if word not in wordList:
        return False
    if len(set(word) - hand) != 0:
        return False
    return True

是不是
-1
?只需
hand.get(k,-1)
即可。是否检查
hand.get(k,-1)
是否小于或大于某个值?你的解释与你的代码不符。@internet\u用户我喜欢你解决OP XY问题的方式。这又让我想得太多了。有时候,你只需要其他人指出一个明显的问题:在这种情况下,我们可以“返回(单词列表中的单词和(len(set(word)-hand)==0)”吗?
if hand.get(k) is None or hand.get(k) < v:
    ...
if k not in hand or hand[k] < v:
    ... 
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: set of letters
          eg: set(["u", "s", "a"] or set("usa")
    wordList: set of strings
          eg: set(["zeus", "osiris", "thor"])
    """
    if word not in wordList:
        return False
    if len(set(word) - hand) != 0:
        return False
    return True