你需要什么具体的输出?我不知道你到底需要做什么,但如果你想把同一个单词相关的条目分组,你可以把这个结构转换成字典,以后再操作它>>> new = {}
>>> for i,j in a: # < a = listaPalabras
if new.get(i) == None:
new[i] = [j]
else:
new[i].append(j)
这将给我们:
^{pr2}$
以后你可以:>>> for i in new:
if len(new[i]) > 1:
print "compare {this} and {that}".format(this=new[i][0],that=new[i][1])
将打印:compare NP00000 and RG #for key bien
编辑:
在第一步中,还可以使用defaultdict,正如Marcin在评论中建议的那样,如下所示:>>> d = defaultdict(list)
>>> for i,j in a:
d.setdefault(i,[]).append(j)
EDIT2(对OP评论的回答)for i in d:
item = []
item.append(i)
item.extend(d[i])
item.append(len(d[i]))
result.append(item)
这给了我们:[['francisco', 'NP00000', 1], ['ser', 'VSIS3S0', 1], ['cosa', 'NCFS000', 1], ['ya', 'RG', 1], ['bien', 'NP00000', 'RG', 2], ['estar', 'VAIP1S0', 1], ['calcio', 'NCMS000', 1], ['leche', 'NCFS000', 1], ['huevo', 'NCMS000', 1], ['gracia', 'NCFP000', 1], ['proteina', 'NCFS000', 1]]