如果你想保持每个非数字单词的第一次出现,这应该可以做到。你总是可以欺骗条件,迫使有最多两位数字。
def cleanup(s):
words = set()
for (word, nextword) in zip(s.split(), s.split()[1:] + [None]):
if word.isdigit():
yield word
continue
if not word in words:
words.add(word)
yield word
elif nextword and nextword.isdigit():
yield word
print ' '.join(cleanup("East Zone Mbc26 East Zone 1 2nd S11B Smds Smoke Damper 1 Status"))
print ' '.join(cleanup("GF Command Room 1 Unit 1 Flow Temperature Temperature"))
产量
East Zone Mbc26 Zone 1 2nd S11B Smds Smoke Damper 1 Status
GF Command Room 1 Unit 1 Flow Temperature