사전에 특정 키와 값 쌍이 있는지 확인하려고합니다. 그러나 contains 또는 has-key 메서드를 사용하면 키를 확인하기 만합니다. 키와 특정 값을 모두 확인해야합니다. 배경 : A, B, CompareList 및 ChangeList에 대해 총 4 개의 사전이 있습니다. 일단 A가 초기화되면 A의 내용을 CompareList에 저장합니다 (직접 비교할 것입니다. 그러나 A와 B는 이중 해시 테이블이며 모든 메서드를 시도했지만 그 중 아무 것도 작동하지 않습니다). 따라서 A를 CompareList에 넣으면 B의 ObjectAttributes 사전과 비교하여 변경된 사항이 있는지 확인합니다. 예를 들어, B는 키, 값 쌍 모양을 가질 수 있습니다 : circle 및 fill : no. CompareList에 shape : circle 및 fill : yes가 있으면 ChangeList 만 fill : yes로 지정합니다. 문제는 "if attribute.getName() not self.CompareList :"행에 있습니다. 다음은 코드입니다. Python 2.7.8에서 실행하고 있습니다. 사전에 어떤 도움을 주셔서 감사합니다 !! 파이썬 3 d.items()
에서특정 키와 값이 사전에 있는지 확인하십시오.
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 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"
사전에 키가 있고 사전에 [key] == value'가있는 g? – Aprillion
이런 식의 문제 (그리고 이전에 지정 했어야 했음)는 반복문을 많이 사용하기 때문에 if 문에 "and"를 사용하면 시간이 늘어납니다. 가능한 한 많이 최적화해야합니다. 이미 복잡한 문제에 대한 추가 오버 헤드가 나쁘다. 도와 줘서 고맙다. –
정말요? 새로운 시도/예외 스택을 만드는 것 (받아 들인 대답 에서처럼)은 간단한 작은'and' 연산자보다 더 빠릅니까? 키가 거의 항상 이미 존재하거나 아마도 컴파일러가 그것을 더 잘 최적화 할 수 있다고 생각합니다. 프로파일 링 PLZ와의 타이밍 차이는 무엇입니까? - 크기 순서의 개선입니까? – Aprillion