擅长:python、mysql、java
<p>只有当属性为<code>x</code>的<em><code>en</code>元素的数目等于<code>"PERS"</code></em>时,才可以添加一个简单的<code>if</code>检查来递增和打印:</p>
<pre><code>for phrase in root.findall('./PHRASE'):
# get all inner text of elements where `x` attribute equals `"PERS"`
names = [p.text.strip() for p in phrase.findall('./en[@x="PERS"]')]
# if therea are 2 of them, increment counter and print
if len(names) == 2:
PERS_PERScount += 1
print('-'.join(names))
print("Number of PERS-PERS relation: ", PERS_PERScount)
</code></pre>
<p><strong><kbd><a href="https://eval.in/531061" rel="nofollow">^{<cd5>}</a></kbd></strong></p>
<p><strong>输出:</strong></p>
<pre><code>John-Alex
Nick-Anna
Number of PERS-PERS relation: 2
</code></pre>