小编Eli*_*tts的帖子

检查字典中是否存在特定的键和值

我试图确定字典中是否存在特定的键和值对;但是,如果我使用contains或has-key方法,它将仅检查密钥。我需要它来检查密钥和特定值。背景知识:我们总共有4个词典:A,B,CompareList和ChangeList词典。初始化A之后,我将A的内容放入CompareList(我将直接比较它们;但是A和B是双哈希表。并且我在这里尝试了所有方法;但是它们对我都不起作用)。因此,一旦将A放入CompareList中,我就将其与B中的ObjectAttributes字典进行比较,以查看是否有任何更改。因此,例如,B可能具有键,值对shape:circle和fill:no。如果CompareList具有shape:circle和fill:yes,那么我只希望fill:yes是ChangeList。问题出在“ if属性。

class ObjectSemanticNetwork:
    def __init__(self):
        self.ObjectNames = {}
        self.ObjectAttributes = {}

    def setName(self, name):
        self.ObjectNames[name] = self.ObjectAttributes

    def setData(self, name, attribute):
        self.ObjectAttributes[name] = attribute

    def checkData(self, key):
        print(key)
        for key, value in self.ObjectAttributes.iteritems():
            print(key)
            print(value)
            print("\n")
class Agent:
(self):
        self.CompareList = {}
        self.ChangeListAB = {}
        self.ChangeListCD = {}

    def addToCompareList(self, name, value):
        self.CompareList[name] = value

    def addToChangeListAB(self, name, value):
        self.ChangeListAB[name] = value

    def addToChangeListCD(self, name, value):
        self.ChangeListCD[name] = value

    def CheckList(self, List, ListName):
        print '-------------------------',ListName,'--------------------------------'
        for key, value in …
Run Code Online (Sandbox Code Playgroud)

python dictionary

0
推荐指数
2
解决办法
2万
查看次数

标签 统计

dictionary ×1

python ×1