擅长:python、mysql、java
<p>输出顺序重要吗?这是我能想到的最简单的方法:</p>
<pre><code>ListA = [[('N1_10', 'N2_28'), ('N1_35', 'N2_44')],[('N1_22', 'N3_72'), ('N1_10', 'N3_98')],
[('N2_33', 'N3_28'), ('N2_55', 'N3_62'), ('N2_61', 'N3_37')]]
idx = dict()
for sublist in ListA:
for pair in sublist:
for item in pair:
mapping = idx.get(item,set())
mapping.update(pair)
idx[item] = mapping
for subitem in mapping:
submapping = idx.get(subitem,set())
submapping.update(mapping)
idx[subitem] = submapping
for x in set([frozenset(x) for x in idx.values()]):
print list(x)
</code></pre>
<p>输出:</p>
^{pr2}$