2014-11-04 81 views
0

我有数百个xml文件。我有函数来比较2 xml树,并返回true,如果它们是相同的。每个xml树都有唯一的id号,在比较中被忽略。将树保存为字典中的键,Python 3.4

现在我可以迭代所有的xml文件并进行比较。但我想将这些树保存在像字典这样的数据结构中。但python不允许我将树保存为一个关键字,并将其id作为一个值。 有没有办法让树的字典作为关键?如果不是,那么可以使用什么类型的数据结构呢?

例如: enter image description here

注意树1 = Tree2但是= Tree3(忽略ID),所以我想我的DIC或任何数据结构是这样的:!

dic = {Tree1:[I1.i1.p1.m1, I1.i1.p1.m2], Tree3: [I1.i1.p1.m3]} 

感谢

+0

你能提供一个**文本**例子和一些实际的代码吗?通常,对象必须实现'__hash__'和'__eq__'作为字典键。 – jonrsharpe 2014-11-04 12:27:39

+0

并非所有内容都可以是字典键,请阅读http://www.tutorialspoint.com/python/python_dictionary.htm搜索页面中的“限制”。 – 2014-11-04 14:08:58

回答

0

字典是HashMap。这意味着键必须是可哈希的,这通常意味着要被哈希的对象是不可变的(为什么列表不是有效键,而是元组)。

你需要的是一个为你的对象生成哈希的函数。在树形数据结构上生成散列是一个不小的问题。但是,既然您已经可以制定平等,则您必须了解一些可以使您的数据可识别的功能。

您始终可以在特征向量上构建散列。可以使用的功能:

  1. 树的深度
  2. 儿童
  3. 散在已经可用的序列化
0

这里的通用解决方案,让说,如果2个XML树是数除特定属性外,其他内容完全相同

import xml.etree.ElementTree as ET 

xml1 = '<?xml version="1.0" encoding="utf-8" ?><Math mode="inline" tau="tex" xml:id="foo"><XMath>2x+3c</XMath></Math>' 
xml2 = '<Math mode="inline" tau="tex" xml:id="bar"><XMath>2x+3c</XMath></Math>' 

#see for more informations https://docs.python.org/3.4/library/xml.etree.elementtree.html 

def almost_equals(tree1, tree2, attributes_to_ignore): 
    """ Return true or false depending on the fact that tree1 and tree2 are identical except for the attributes whose the tag is in attributes to ignore. """ 
    #remove attributes to ignore 
    for attribute in attributes_to_ignore: 
     try: 
      tree1.attrib.__delitem__(attribute) 
     except: 
      pass  
     try: 
      tree2.attrib.__delitem__(attribute) 
     except: 
      pass 

    #compare nodes 
    if tree1.tag != tree2.tag: 
     print(tree1.tag,"!=",tree2.tag) 
     return False 

    if tree1.attrib != tree2.attrib: 
     print(tree1.attrib,"!=",tree2.attrib) 
     return False 

    if tree1.text != tree2.text: 
     print(tree1.text,"!=",tree2.text) 
     return False 

    subtrees1 = list(tree1) 
    subtrees2 = list(tree2) 

    if len(subtrees1) != len(subtrees2): 
     return False 

    result = True 
    for i in range(len(subtrees1)): 
     result = result and almost_equals(subtrees1[i], subtrees2[i], attributes_to_ignore) 

    return result 

if __name__ == "__main__": 
    xmlTree1 = ET.fromstring(xml1) 
    xmlTree2 = ET.fromstring(xml2) 
    print("The 2 xml trees are identical ({0})".format(almost_equals(xmlTree1, xmlTree2, ["{http://www.w3.org/XML/1998/namespace}id"]))) 

希望它有帮助。 亚瑟。

编辑:您可以将XML保存为xml并根据需要解析它们,或者节省由内置python库生成的Element对象。