考虑以下示例,

我需要通过比较所有邻居标签的name属性来检查任何两个国家是否具有相同的邻居。
我正在使用.attrib,然后比较字典,到目前为止,它是基于key的.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。

最佳答案

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

10-06 01:16