Check if a specific Key and a value exist in a dictionary
我试图确定字典中是否存在特定的键和值对;但是,如果我使用contains或has key方法,它只检查键。我需要它来检查键和特定值。一些背景:我们总共有4个字典:一个用于A、B、CompareList和ChangeList。一旦A被初始化,我将A的内容放入比较列表(我将直接比较它们;但是A和B是双哈希表)。我试过这里所有的方法,但没有一个对我有用)。所以一旦我们将a放入comparelist中,我将它与b中的objecttributes字典进行比较,看看是否有什么变化。例如,B可能有键,值对shape:circle和fill:no。如果compareList有shape:circle和fill:yes,那么我只希望fill:yes是changelist。问题在于"if attributes.getname()不在self.compareList中:"行。这是代码;我在Python2.7.8上运行它。提前感谢您的帮助!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 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(" ") 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 List.iteritems(): print(key) print(value) def Solve(self,problem): OSNAB = ObjectSemanticNetwork() for object in problem.getFigures().get("A").getObjects(): for attributes in object.getAttributes(): self.addToCompareList(attributes.getName(), attributes.getValue()) OSNAB.ObjectNames["A"] = OSNAB.setData(attributes.getName(), attributes.getValue()) #OSNAB.checkData("A") self.CheckList(self.CompareList,"CompareList") for object in problem.getFigures().get("B").getObjects(): for attributes in object.getAttributes(): if attributes.getName() not in self.CompareList: self.addToChangeListAB(attributes.getName(), attributes.getValue()) OSNAB.ObjectNames["B"] = OSNAB.setData(attributes.getName(), attributes.getValue()) # OSNAB.checkData("B") self.CheckList(self.ChangeListAB,"ChangeList") OSNCD = ObjectSemanticNetwork() for object in problem.getFigures().get("C").getObjects(): for attributes in object.getAttributes(): OSNCD.ObjectNames["C"] = OSNCD.setData(attributes.getName(), attributes.getValue()) # OSNCD.checkData("C") for object in problem.getFigures().get("1").getObjects(): for attributes in object.getAttributes(): OSNCD.ObjectNames["D"] = OSNCD.setData(attributes.getName(), attributes.getValue()) # OSNCD.checkData("D") return"6" |
使用
1 | if key in d and d[key] == value: |
或(仅在python 3中)
1 | if (key, value) in d.items(): |
在python 3中,
编辑:在评论中,您指出出于性能原因,您更喜欢
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | >>> import random >>> from timeit import timeit >>> def checkKeyValuePairExistence(dic, key, value): ... try: ... return dic[key] == value ... except KeyError: ... return False ... >>> d = {random.randint(0, 100000):random.randint(0, 100000) for i in range(1000)} >>> setup = 'from __main__ import k, d, v, checkKeyValuePairExistence' >>> test_try_except = 'checkKeyValuePairExistence(d, k, v)' >>> test_k_in_d_and = 'k in d and d[k] == v' >>> k, v = random.choice(d.items()) # to test if found >>> timeit(test_try_except, setup=setup) 0.1984054392365806 >>> timeit(test_k_in_d_and, setup=setup) 0.10442071140778353 >>> k = -1 # test if not found >>> timeit(test_try_except, setup=setup) 1.2896073903002616 >>> timeit(test_k_in_d_and, setup=setup) 0.07827843747497809 |
这个功能怎么样:
1 2 3 4 5 | def checkKeyValuePairExistence(dic, key, value): try: return dic[key] == value except KeyError: return False |
如果你使用的是另一种类型的字典,那么python提供的字典(对不起,如果你使用与否,我无法从你的帖子中理解),那么请告诉我,我将尝试给出另一种解决方案。
为什么不这么做:
1 2 3 | a = {1:'a', 2:'b'} b = (1, 'a') print b in a.iteritems() # prints True |