Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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_Python 3.x - Fatal编程技术网

Python 检查键以给定字符串开头的所有字典值是否存在于一行中

Python 检查键以给定字符串开头的所有字典值是否存在于一行中,python,python-3.x,Python,Python 3.x,我当前的编码方式不起作用,因为all返回false,因为在循环key.startswith时,在对非所需的键进行迭代期间,它会标记为false 本质上,我想要检查的是,由传递的字符串作为前缀的所有键是否都有一个值。我想试着用一句话来迎接我自己的挑战,并不是说这样做一定更好,而是感觉被卡住了。实现这一目标的最佳方式是什么 class MyPlayer(): def __init__(self): self.player_attributes = { &quo

我当前的编码方式不起作用,因为all返回false,因为在循环key.startswith时,在对非所需的键进行迭代期间,它会标记为false

本质上,我想要检查的是,由传递的字符串作为前缀的所有键是否都有一个值。我想试着用一句话来迎接我自己的挑战,并不是说这样做一定更好,而是感觉被卡住了。实现这一目标的最佳方式是什么

class MyPlayer():
    def __init__(self):
        self.player_attributes = {
        "one_height": "5'8",
        "one_weight": "150",
        "one_age": "25",
        "two_height": "5'11",
        "two_weight": "160",
        "two_age": None, 
        }
    def check_if_all_values_exist(self, prefix_to_check):
        if all([key.startswith(prefix_to_check) and self.player_attributes[key] != None for key in self.player_attributes.keys()]):
            print("true")

Test = MyPlayer()
Test.check_if_all_values_exist("one")
Test.check_if_all_values_exist("two")

首先,您需要更仔细地考虑您的条件:只有当一个键确实以标记字符串开头,并且它没有值时,您才会遇到麻烦。你需要应用DeMorgan定理。我将其编码为更易于使用的返回布尔值,但我保留了打印特性以跟踪结果

...
    def check_if_all_values_exist(self, prefix_to_check):
        result = all([not key.startswith(prefix_to_check) or
                      self.player_attributes[key] != None
                for key in self.player_attributes.keys()])
        print(prefix_to_check, result)

Test = MyPlayer()
Test.check_if_all_values_exist("one")
Test.check_if_all_values_exist("two")
输出:

one True
two False
one, True
two, False

忽略所有类和其他部分,只看函数,下面是一行代码

return all(v != None for k,v in player_attributes.items() if k.startswith(x))
永远记住过滤掉噪音,这样你就可以专注于真正需要的东西。然后你就很容易决定你是否得到了你需要的结果。在上面的循环中,for循环将过滤掉所有键(如果它们不是以前缀\u to\u check开头)。然后就很容易看到值是否为None

完整的功能定义将是:

def check_key(x):
    player_attributes = {
    "one_height": "5'8",    
    "one_weight": "150",
    "one_age": "25",
    "two_height": "5'11",
    "two_weight": "160",
    "two_age": None, 
    }

    return all(v != None for k,v in player_attributes.items() if k.startswith(x))

print ('one', check_key('one'))
print ('two', check_key('two'))
输出:

one True
two False
one, True
two, False

如果所有[key.startswithprefix\u to\u check和self.player\u attributes[key]!=None用于输入self.player\u attributes.keys]:这意味着:对于所有键,您将检查键是否以前缀开头且具有非None值。您想要的是:对于所有键,如果它以前缀开头,则它具有非None值。或者换句话说:对于所有键,键具有非None值或不以前缀开头。你明白为什么这在逻辑上是等价的吗?知道了这一点,您可以编写代码吗?如何思考:当您试图解决遍历列表的问题时,重要的是要考虑在单个元素级别要做什么。如果要进行all检查,则需要考虑给定元素应为真的条件。什么是允许的,什么是不允许的?显然,它不必以前缀开头。如果它以前缀开头,那么什么应该是真的?如果没有,还有其他限制吗?当我问自己这些问题时,我在另一条评论中提出了规则。return allv!=如果k.startswithxRight,则player_attributes.items中的k、v为无;另一种方法是过滤所有人都考虑的关键点。“事实上,这样更好。”卡尔克内切特谢谢!通过将if语句改为if all[key.startswithprefix\u to\u check==False或self.player\u attributes[key]!=None for key-in-self.player\u attributes.keys]:发布的其他解决方案似乎比我的更好,因此我将尝试从中学习。