如何從字符串中刪除重複的行,然後打印刪除的行數量?計數刪除行
我得到這個:
import os
sentence = """Sentence1
Sentence1
Sentence2
Sentence3
Sentence4
Sentence4"""
spaces = sentence.replace(" ", "\n") #Makes one word per line
lines = os.linesep.join([s for s in spaces.splitlines() if s]) #Removes empty lines
duplicate = "\n".join(set(lines.split('\n'))) #Removes duplicate lines
numberlines = len(duplicate.split('\n')) #Counts lines
print(duplicate)
print'Lines:', numberlines
這樣,輸出是:
Sentence4
Sentence1
Sentence2
Sentence3
Lines: 4
我怎樣才能達到這個輸出:
Sentence4
Sentence1
Sentence2
Sentence3
Lines: 4
Removed Lines: 2
感謝:d
你能算上之前的長度差' - after'? – fredtantini
現在正在處理這個問題,或許實際上可以解決這個問題。雖然我是python的新手。 – CandyGum