考虑以下示例,
我需要通过比较所有邻居标签的name
属性来检查任何两个国家是否有相同的邻居。
我正在使用.attrib
然后比较字典,该字典是基于密钥的.attrib的输出。有没有更简单的方法来检查属性?
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
我在python中使用lxml。
答案 0 :(得分:1)
for every country as c
for every neighbor of c as n
neighbors[n].push(c)
for every neighbor in neighbors
if count(neighbors[neighbor]) > 1
ding ding ding