将树另存为字典中的键,Python 3.4
我有数百个xml文件。我有一个比较两个xml树的函数,如果它们相同,则返回true。 每个xml树都有唯一的id号,在比较中会被忽略 现在我可以迭代所有xml文件并比较它们。但是我想把树保存在数据结构中,比如字典。但python不允许我将树保存为键,将其id保存为值。 有没有办法把树的字典作为键?如果不是,那么什么类型的数据结构可以用于此 例如: 请注意,Tree1=Tree2,但是!=Tree3(忽略id),因此我希望我的dic或任何数据结构如下:将树另存为字典中的键,Python 3.4,python,xml,dictionary,tree,Python,Xml,Dictionary,Tree,我有数百个xml文件。我有一个比较两个xml树的函数,如果它们相同,则返回true。 每个xml树都有唯一的id号,在比较中会被忽略 现在我可以迭代所有xml文件并比较它们。但是我想把树保存在数据结构中,比如字典。但python不允许我将树保存为键,将其id保存为值。 有没有办法把树的字典作为键?如果不是,那么什么类型的数据结构可以用于此 例如: 请注意,Tree1=Tree2,但是!=Tree3(忽略id),因此我希望我的dic或任何数据结构如下: dic = {Tree1:[I1.i1.p
dic = {Tree1:[I1.i1.p1.m1, I1.i1.p1.m2], Tree3: [I1.i1.p1.m3]}
谢谢,字典是散列图。这意味着键必须是可散列的,这通常意味着要散列的对象是不可变的(为什么列表不是有效键,而元组是有效键) 您需要的是一个为您的对象生成此哈希的函数。在树状数据结构上生成散列是一个非常重要的问题。但是,既然您已经可以表述等式,那么您必须了解使数据可识别的一些特性 您始终可以在特征向量上构建哈希。可以使用的功能:
这里是一个通用的解决方案,它允许说明除了某些属性外,两个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.etree.ElementTree作为ET导入
xml1='2x+3c'
xml2='2x+3c'
#有关更多信息,请参阅https://docs.python.org/3.4/library/xml.etree.elementtree.html
def几乎等于(tree1、tree2、属性到忽略):
“”“返回true或false,这取决于tree1和tree2除了标记位于要忽略的属性中的属性之外是相同的事实。”“”
#删除要忽略的属性
对于属性_到_忽略中的属性:
尝试:
树1.attrib.\uuu delitem\uuuu(属性)
除:
通过
尝试:
tree2.attrib.\uuu delitem\uuuu(属性)
除:
通过
#比较节点
如果树1.tag!=tree2.tag:
打印(tree1.tag,“!=”,tree2.tag)
返回错误
如果树1.attrib!=tree2.attrib:
打印(tree1.attrib,“!=”,tree2.attrib)
返回错误
如果树1.text!=tree2.text:
打印(tree1.text,“!=”,tree2.text)
返回错误
子树1=列表(树1)
子树2=列表(树2)
如果len(子树1)!=len(子树2):
返回错误
结果=真
对于范围内的i(len(子树1)):
结果=结果且几乎等于(子树1[i],子树2[i],属性\u至\u忽略)
返回结果
如果名称=“\uuuuu main\uuuuuuuu”:
xmlTree1=ET.fromstring(xml1)
xmlTree2=ET.fromstring(xml2)
print(“两个xml树是相同的({0})”.format(几乎等于(xmlTree1,xmlTree2,[){http://www.w3.org/XML/1998/namespace}id“]))
希望能有帮助。
亚瑟
编辑:您可以将XML另存为XML并按需解析,也可以svae内置python库生成的元素对象。能否提供一个文本示例和一些实际代码?一般来说,对象必须实现
\uuuuuu hash\uuuuuu
和\uuuuuuuu eq\uuuuuu
才能用作字典键。并非所有对象都可以是字典键,请阅读第页中的搜索“限制”。