使用sets
:
单线:
>>> seen = set()
>>> [ [y for y in x if y not in seen and not seen.add(y)] for x in a]
[[('bring', 'VBG'), ('puffy', 'NN'), ('eyes', 'NNS'), ('back', 'RB')], [('droopy', 'NN')]]
上面的代码等价于:
>>> a = [[('bring', 'VBG'), ('puffy', 'NN'), ('eyes', 'NNS'), ('back', 'RB')], [('droopy','NN'), ('eyes', 'NNS')]]
>>> seen = set()
>>> out_lis = []
for x in a:
temp = []
for y in x:
if y not in seen:
seen.add(y)
temp.append(y)
out_lis.append(temp)
...
>>> out_lis
[[('bring', 'VBG'), ('puffy', 'NN'), ('eyes', 'NNS'), ('back', 'RB')], [('droopy', 'NN')]]